In the early days of PHP programming, PHP code was limited to being procedural in nature. Procedural code is characterized by the use of procedures for the building blocks of the application. Procedures offer a certain level of reuse by allowing procedures to be called by other procedures.
However, without object-oriented language constructs, a programmer can still introduce OO characteristics into PHP code. It's a tad more difficult and can make the code more difficult to read because it's mixing paradigms (procedural language with pseudo-OO design). OO constructs in PHP code - such as the ability to define and use classes, the ability to build relationships between classes that use inheritance, and the ability to define interfaces - make it much easier to build code that adheres to good OO practices.
While purely procedural designs without much modularity run just fine, the advantages of OO design show up in the maintenance. Because a typical application will spend the bulk of its lifetime in maintenance, code maintenance is a large expense over the lifetime of an application. It can also be easily forgotten during development. If you're in a race to get your application developed and deployed, long-term maintainability can take a back seat to getting something to work.
Modularity - one of the key characteristics of good OO design - helps with this maintenance. Modularity helps encapsulate change, which will make it easier to extend and modify the application over time.
While there are more than seven habits to building OO software overall, the seven habits here are what you need to make your code fit basic OO design criteria. They give you a firm foundation upon which you can add more OO habits and build software that is easily maintained and extended. These habits target a couple of the key characteristics of modularity.
The seven good PHP OO habits are:
- 1. Be modest.
- 2. Be a good neighbor.
- 3. Avoid looking at Medusa.
- 4. Embrace the weakest link.
- 5. You're rubber; I'm glue.
- 6. Keep it in the family.
- 7. Think in patterns.