PHP: Object Inheritance

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

2081 views

Edited: 2020-04-20 15:55

PHP article image

Inheritance allows one class (the child class) to extend another class (the parent), and keep all of the properties and methods of the parent class.

This is probably mostly useful when working on other peoples code rather than your own. Using it when designing in your own objects will most often make your code inflexible and hard to maintain. Do not use inheritance to split up and organize your code into multiple classes, instead, consider using Dependency Injection.

Variables that are private will not be accessible from child-classes, if you want to prevent outside access to properties or methods while still allowing access from child-classes, use protected instead.

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;
  
    public function set_data ($input) {
        $this->some_data = $input;
    }
}
class second_class extends first_class {
  
    public 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();

Tell us what you think:

  1. In this Tutorial, it is shown how to redirect all HTTP requests to a index.php file using htaccess or Apache configuration files.
  2. How to create a router in PHP to handle different request types, paths, and request parameters.
  3. Tutorial on how to use proxy servers with cURL and PHP
  4. When using file_get_contents to perform HTTP requests, the server response headers is stored in a reserved variable after each successful request; we can iterate over this when we need to access individual response headers.
  5. How to effectively use variables within strings to insert bits of data where needed.

More in: PHP Tutorials