News & UpdatesProgrammingWeb programming StoreMy Projects
Links
Affiliates

PHP Tutorial – 11 – Inheritance

Inheritance allows a class to acquire the members of another class. In the example below the class Square inherits from Rectangle, specified by the extends keyword. Rectangle then becomes the parent class of Square, which in turn becomes a child class of Rectangle. In addition to its own members, Square gains all accessible (non-private) members in Rectangle, including any constructor.

// Parent class (base class)
class Rectangle
{
  public $x, $y;
  function __construct($a, $b) 
  { 
    $this->x = $a; 
    $this->y = $b; 
  }
}
 
// Child class (derived class)
class Square extends Rectangle {}

When creating an instance of Square two arguments must now be specified, because Square has inherited Rectangle’s constructor.

$s = new Square(5,10);

The properties inherited from Rectangle can also be accessed from the Square object.

$s->x = 5; $s->y = 10;

A class in PHP may only inherit from one parent class and the parent must be defined before the child class in the script file.

Overriding members

A member in a child class can redefine a member in its parent class to give it a new implementation. To override an inherited member it just needs to be redeclared with the same name. As shown below, the Square constructor overrides the constructor in Rectangle.

class Square extends Rectangle
{
  function __construct($a) 
  { 
    $this->x = $a; 
    $this->y = $a; 
  }
}

With this new constructor only a single argument is used to create the Square.

$s = new Square(5);

Because the inherited constructor of Rectangle is overridden, Rectangle’s constructor will no longer be called when the Square object is created. It is up to the developer to call the parent constructor if necessary. This is done by prepending the call with the parent keyword and a double colon. The double colon is known as the scope resolution operator (::).

class Square extends Rectangle
{
  function __construct($a) 
  { 
    parent::__construct($a,$a); 
  }
}

The parent keyword is an alias for the parent’s class name, which may alternatively be used. In PHP, it is possible to access overridden members that are any number of levels deep in the inheritance hierarchy using this notation.

class Square extends Rectangle
{
  function __construct($a) 
  { 
    Rectangle::__construct($a,$a); 
  }
}

Like constructors, the parent destructor will not be called implicitly if it is overridden. It too would have to be explicitly called with parent::__destruct() from the child destructor.

Final keyword

To stop a child class from overriding a method it can be defined as final. A class itself can also be defined as final to prevent any class from extending it.

final class NotExtendable
{
  final function notOverridable() {}
}

Instanceof operator

As a safety precaution, you can test to see whether an object can be cast to a specific class by using the instanceof operator. This operator returns true if the left side object can be cast into the right side type without causing an error. This is true when the object is an instance of, or inherits from, the right side class.

$s = new Square(5);
$s instanceof Square;    // true
$s instanceof Rectangle; // true
Recommended additional reading:
Sams - Teach Yourself PHP in 24 Hours