jump to navigation

Understanding Inheritance in PHP October 11, 2010

Posted by Tournas Dimitrios in PHP.
trackback

Welcome to another part of the serie ” The basics of OOP with PHP ” follow up …

One great aspect of using classes is that you can extend an existing class by creating a new class that “inherits” all the properties and methods of the parent class. The example below demonstrates this concept:

< ? p h p

Class MyCar {
        public $name;

        function __construct($name) {
                $this->name = $name;
        }

        function drive() {
                echo 'Vroooom... ' . $this->name;
        }
}

Class PorscheCar Extends MyCar {
        // Contains no properties or methods of its own
}

$boxster = new PorscheCar('Boxster 987');
$boxster->drive();

The ‘PorscheCar’ class contains no methods or properties at all, but we can still use the drive() method, because the PorscheClass inherits all the properties and methods from its parent class ‘MyCar’. To inherit from another class you need to use the Extend keyword, as you can see in the example.

It’s also possible to override methods from the parent class by redefining the method in the child class. This can be used to create a generic parent class, and then several specialized child classes. The example below demonstrates how to override a method from the parent class:

< ? p h p

Class MyCar {
        public $name;

        function __construct($name) {
                $this->name = $name;
        }

        function drive() {
                echo 'Vroooom... ' . $this->name;
        }
}

Class PorscheCar Extends MyCar {

        function drive() {
                echo 'VROOOOOOOOM... ' . $this->name;
        }

}

$boxster = new PorscheCar('Boxster 987');
$boxster->drive();

 

As you can see we’ve redefined the drive() method to print a different message.

One of PHP5’s new features is that it’s possible to extend methods from the parent class, instead of completely redefining them. To do this you will have to use the ‘parent’ constant, like this:


< ? p h p

Class MyCar {
 public $name;

function __construct($name) {
 $this->name = $name;
 }

function drive() {
 echo 'Vroooom... ' . $this->name;
 }
 }

Class PorscheCar Extends MyCar {

function drive() {
 echo 'VROOOOOOOOM... ' . $this->name;
 echo '';

// Call original method from parent class
 parent::drive();
 }

}

$boxster = new PorscheCar('Boxster 987');
 $boxster->drive();

The result of the above example will look like this:

VROOOOOOOOM… Boxster 987
Vroooom… Boxster 987

By using the ‘parent’ constant it’s possible to call the original methods, and extend them. Please be aware that the parent constant only exists in a child class, which actually extends another class, and doesn’t exist in normal functions or a class that doesn’t extend any other class.

 

 

Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s