Inheritance allows a class to acquire the members of another class. For example, the Square class 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 re-declared with the same name.

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 is no longer called when the Square object is created. It is up to the developer to call the parent constructor, if necessary. This is done by 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);
}
}

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

Access Levels

Every class member has an accessibility level that determines where the member is visible. There are three of them available in PHP: public, protected, and private.

class MyClass
{
public $myPublic; // unrestricted access
protected $myProtected; // enclosing or child class
private $myPrivate; // enclosing class only
}

Unlike properties, methods do not have to have an explicit access level specified. They default to public access unless set to another level.

As a guideline , when choosing an access level, it is generally best to use the most restrictive level possible. This is because the more places a member can be accessed, the more places it can be accessed incorrectly, which makes the code harder to debug. Using restrictive access levels also makes it easier to modify the class without breaking the code for any other developers using that class.

Private Access

All members, regardless of access level, are accessible in the class in which they are declared - the enclosing class. This is the only place where a private member can be accessed.

Protected Access

A protected member can be accessed from inside the child or the parent classes, as well as from within the enclosing class.

Public Access

Public members have unrestricted access. In addition to anywhere a protected member can be accessed, a public member can also be reached through an object variable.

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive