ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

PHP5 Class Basics, Part 1

(Page 3 out of 3)

Inheritance

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:

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();

?>

(View Live Demo)

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:

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();

?>

(View Live Demo)

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:

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.

Conclusion

In this first part on class/object basic in PHP5 I've taken you through the absolute basics of classes, explained the constructor/destructor and shown you how inheritance works. Although we haven't looked at more advanced topics yet, the things we discussed in this article are probably the things you'll use most of the time, and are quite important.

In the next part we'll take a look at the visibility modifiers for properties and methods, class constants, and static classes.

If you have any comments or questions, please leave them in the comments below.

« Previous: Constructor/Destructor



One Response to “PHP5 Class Basics, Part 1”

  1. PHPDeveloper.org Says:

    PHPit.net: PHP5 Class Basics, Part 1…

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & Basic Concepts
  2. Constructor/Destructor
  3. Inheritance & Conclusion
Bookmark Article
Download Article
PDF
Download this article as a PDF file