Object Reuse and Inheritance

In a biological sense, a child inherits genes from its parents, and this genetic material conditions the appearance and behavior of the child. In OOP the meaning of inheritance is analogous—it is the ability to pass along characteristics and behavior. At first this feature of OOP may seem somehow magical, but really inheritance is just a technique for reusing code—much the way you might include a library of functions in procedural programming.

If you identify an existing class that exactly suits your needs, you can simply use it and benefit from the predefined behavior. Inheritance comes into play when a class doesn't do quite what you want. This situation is not much different from adding functions to an existing ...

Get Object-Oriented PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.