User Contributed Notes 13 notes

Here is some clarification about PHP inheritance – there is a lot of bad information on the net. PHP does support Multi-level inheritance. (I tested it using version 5.2.9). It does not support multiple inheritance.

This means that you cannot have one class extend 2 other classes (see the extends keyword). However, you can have one class extend another, which extends another, and so on.

Example:

<?phpclass A {// more code here}

class B extends A {// more code here}

class C extends B {// more code here}

$someObj = new A(); // no problems$someOtherObj = new B(); // no problems$lastObj = new C(); // still no problems

Even when autoloading (SPL) is used, class inheritance does not seem to work. Simply the PHP engine is unable to find parent (inherited) class. PHP 5.6 and 7.0 behave exactly same on this, which beats the purpose of autoloading.

And IMHO it's easy to fix as the autoloader is able to find all first level classes w/o problems, it just needs to follow same path recursively on parents too.

<?php//Using default SPL autoloader, with namespaces mapping 1:1 to directory structure, with file names being all lowercase. //This works with first level classes only, for inheritance it does NOT work, it cannot find parent classes.spl_autoload_register();

//This is ugly but working code if you want to be able to autoload parent classes too.spl_autoload_register(function ($class){ require_once __DIR__ . '/' . strtolower(str_replace('\\', '/', $class) . '.php');});

I was recently extending a PEAR class when I encountered a situation where I wanted to call a constructor two levels up the class hierarchy, ignoring the immediate parent. In such a case, you need to explicitly reference the class name using the :: operator.

Fortunately, just like using the 'parent' keyword PHP correctly recognizes that you are calling the function from a protected context inside the object's class hierarchy.

You can force a class to be strictly an inheritable class by using the "abstract" keyword. When you define a class with abstract, any attempt to instantiate a separate instance of it will result in a fatal error. This is useful for situations like a base class where it would be inherited by multiple child classes yet you want to restrict the ability to instantiate it by itself.

I think the best way for beginners to understand inheritance is through a real example so here is a simple example I can gave to you

<?php

class Person{ public $name; protected $age; private $phone;

public function talk(){//Do stuff here}

protected function walk(){//Do stuff here}

private function swim(){//Do stuff here}}

class Tom extends Person{/*Since Tom class extends Person class this means that class Tom is a child class and class person is the parent class and child class will inherit all public and protected members(properties and methods) from the parent class*/

This page is not clear about the nature and specifics of Object Inheritance especially that you can only make the visibility of an inherited method or property weaker and not stronger within the subclass that is inheriting these from its parent class.

Here's fun, an attempt to make some degree of multiple inheritance work in PHP using mixins. It's not particularly pretty, doesn't support method visibility modifiers and, if put to any meaningful purpose, could well make your call stack balloon to Ruby-on-Rails-esque proportions, but it does work.