4.3. Working with Inheritance

Inheritance is a core component of the object-oriented paradigm. With the help of inheritance, it is possible to build short and efficient code. However, if the hierarchy of objects becomes too complex, your applications will be difficult to debug.

Inheritance means that a class can inherit methods and attributes from a parent class. In PHP, a class can only inherit from the parent class—it is not possible for one class to have more than one parent.

After this theoretical overview, you will see an example of a class that inherits from a parent class:

 <?php # Class human class human { var $age; var $gender; # Constructor function human($age, $gender) { $this->age = $age; $this->gender = $gender; } function getage() ...

Get PHP and PostgreSQL: Advanced Web Programming 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.