PHP Inheritance

Nov 19, 13 • PHP TutorialsNo CommentsRead More »

Inheritance in PHP allows for connections between different classes. Child classes can inherit attributes and methods from parent classes, allowing them to become more specialised by extending the functionality of the base class.

For a real world example, you can think of a base class Fruit, with a derived class Apple, which in turn has another derived class Fuji. Each derived class inherits the methods and properties of its parent class, while also providing its own additional attributes/methods. That is to say, Fuji extends Apple which extends Fruit.

This can be represented as:

PHP - Inheritance Hierarchy

As you head further down, the classes become more specialised, inheriting the properties and methods of their parents.

This real world example can be represented programmatically as:

<?php
    
class Fruit
{
    public function hasSeeds()
    {
        return true;
    }
}

class Apple extends Fruit
{
    public function grownOn()
    {
        return 'tree';
    }
}

class Fuji extends Apple
{
    public function color()
    {
        return 'red';
    }
}

We can insantiate our Fuji class thusly:

$fujiApple = new Fuji;
if ($fujiApple->hasSeeds())
    print "Our Fuji Apple has seeds!\n";
    
print "Our Fuji Apple is grown on a: " . $fujiApple->grownOn() . "\n";
print "Out Fuji Apple is of color: " . $fujiApple->color() . "\n"; 

This in turn outputs:

Our Fuji Apple has seeds!
Our Fuji Apple is grown on a: tree
Out Fuji Apple is of color: red

Visibility Keywords

So far, all of our method definitions have been declared as public. This therefore allows all child classes to inherit and access those method/properties, as well as instances of the class (as we saw in our previous input). To control the scope of these properties and methods, we can define them as either public, private or protected.

Protected methods/properties

Methods and properties declared as protected are inherited and are accessible to all derived (child) classes, but not object instances of those classes. If we change the hasSeeds() method in our base Fruit class to protected, then the derived classes Apple and Fuji will be able to access that method, whereas instances of the class cannot.

For instance, with Fruit::setSeeds() set to protected, the following is okay:

 class Fuji extends Apple
{
    public function outputProperties()
    {
        if ($this->hasSeeds())
            print "Our Fuji Apple has seeds!\n";
    }
} 

This however, is not, and will return a fatal error:

 $fujiApple = new Fuji;
if ($fujiApple->hasSeeds())
    print "Fuji Apple has seeds!\n"; 

Private methods/properties

Protected methods are accessible to the class they’re defined in, as well as all derived classes. Private methods and properties on the other hand, are only accessible to the class in which they’re defined. That is, as the name suggests, they are uniquely accessible only in that class.

For instance, if we were to change our Fuji class to:

 class Fuji extends Apple
{
    private function color()
    {
        return 'red';
    }
    
    public function outputProperties()
    {
        if ($this->hasSeeds())
            print "Our Fuji Apple has seeds!\n";
            
        print "Our Fuji Apple is grown on a: " . $this->grownOn() . "\n";
        print "Out Fuji Apple is of color: " . $this->color() . "\n";
    }
} 

Our output would be the same as it were previously, and we are able to reference $this->color() as it is defined in that same class. However, if we were to extend our Fuji class (with perhaps another child strain) and tried to access the color() method, we would receive an error:

 class DomoFuji extends Fuji
{
    public function outputProperties()
    {
        if ($this->hasSeeds())
            print "Our Fuji Apple has seeds!\n";
            
        print "Our Fuji Apple is grown on a: " . $this->grownOn() . "\n";
        print "Out Fuji Apple is of color: " . $this->color() . "\n"; // This line would result in a fatal error
    }
} 

Advantages of Inheritance

Code Reusability

Generally, having identical code spread throughout your code base is indicative of poor design. When tackling a problem using an object-oriented approach, it’s quite common for multiple classes to share identical properties and methods.

If you had classes representing two types of staff members, Casual and FullTime, then it quickly becomes apparent that there are properties and methods that both have in common. For instance, both casual and full time workers would have a first and last name, a social security number, etc. On the other hand, perhaps only full time employees have a dental plan. In this scenario, your FullTime class would extend from your base Staff class, and then extend it with methods and properties related to their dental plan.

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>