O'Reilly logo

Professional PHP Design Patterns by Aaron Saray

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

6.1. Problem and Solution

When just beginning to learn about Object Oriented Programming, the first hurdle usually is understanding the parent-child relationship through inheritance. As time goes on, this method of programming becomes more familiar and easy. When faced with new challenges, even seasoned object-oriented programmers can jump immediately to extending an object to add more functionality. However, as with everything that is great, it's only healthy when used in moderation.

There is a limit to the amount of class hierarchy that a code base should have. If objects start requiring too many children to become functional, the code sacrifices both programmer comprehension and maintainability. Generally, I try not to ever have more than three parent-child relationships for one object. I find when more parent-child relationships are created, the code starts to become confusing and unwieldy. Besides, printing a UML diagram representation of any object in your application hopefully should not require legal-sized paper.

Another reason to be careful when generating complex class hierarchies is PHP's limitation of extending only one class. If a more comprehensive extension model existed in PHP, there might be less chance of creating unwieldy object relationships.

I don't wish to deter you from the usage of class extension, however. There are many times when the proper solution to the problem is extending the object. Even some examples of Design Patterns in this book require objects ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required