By using this site you agree to the use of cookies and related technologies.

Learn more

PHP: Object inheritance

How to extend classes in PHP, and how to use object inheritance.

Edited: 2016-09-24 03:25

In the last tutorial you should have learned how to create basic classes, in this tutorial you will learn how to extend your classes, and how to use object inheritance. When working with OOP in PHP you typically have more than just one class. It is good practice to split your classes in a way that makes sense, and also to not allow your classes to get unnecessarily big. Instead, you can split big classes into multiple smaller ones, and then connect them by extending them as needed.

For example, if you have one big class, with a lot of methods (functions), and you decide to split this class into two classes, you would typically make one of the classes the main class, and then extend it with your new class. Also note, that often you just have one class per PHP file.

Extending a class combines two or more classes into one big class. A more adequate explanation would likely be that the class you connect with your main class, also inherits all of its properties (variables) and methods (functions). Properties and methods from child classes also becomes available inside of the parent class.

Extending classes in PHP

Extending a class is done with the extends keywoard, the class that extends another class becomes a child of that class. I.e.

class first_class {
    public $some_data;
  
    function set_data ($input) {
        $this->some_data = $input;
    }
}
class second_class extends first_class {
  
    function get_data () {
        return $this->some_data;
    }
}

$MyObject = new second_class();

$MyObject->set_data('Just a test'); // Sets the some_data variable
echo $MyObject->get_data(); // Should output "Just a test"

The above extends the first class, which allow us to access all its public properties and methods in the first class, and vise versa. Properties and methods can be declared as public, private and protected. If you declare a property or method without using one of those keywords, it will be declared as public by default.

Class members that are declared as Public can be accessed everywhere, an example would be outside classes with $MyObject->method() for methods, and $MyObject->property for properties. I.e.

$MyObject = new some_class(); // Creates the object from some_class

echo $MyObject->method();
echo $MyObject->property;

Parent classes must be declared before their child classes.

Visibility

The keywords public, private and protected are used to declare properties and methods, and better control their access. Briefly put, if a member is not declared using a keyword, by default it will become public, making it accessible everywhere – including from the outside of the classes.

Public members can be accessed from outside the classes trough ways like $MyObject->property; and $MyObject->method();

Private members can only be accessed within the class where they are declared. In other words, members declared as private are not shared among parents and children.

Protected members are shared among children and their parents, but not accessible outside the classes. For example, a protected method can not be accessed from the outside trough $MyObject->method(); but can be accessed by other methods inside the class trough $this->method();