Inheritance | Object Oriented Concepts in PHP

Inheritance is one of the most important concepts of Object-Oriented Programming. It allows a class to inherit some properties of another class. It comes very useful when we want to build a class upon an existing class. In this article, we will discuss Inheritance in PHP.

If you are new to Object-Oriented Programming, then I would recommend you to go through my Introduction article.

What is Inheritance?

Let us consider a very simple example to understand the concept of Inheritance. Consider the need to represent Animals. Now, all animals like Dogs, Cats etc have a few things in common. They all have four legs, a pair of eyes, a node, a mouth and etc. All the animals will share some functionalities like walking, eating but differ in a few like the sound they make.

The idea is to create a Base Class called Animals. The sub-classes like Dog and Cat will inherit some properties from our base class.

Inheritance in PHP

Like all other languages, PHP makes use of this principle extensively. A child class will inherit all the public and protected members of the parent class. However, private methods are not inherited by the child class. Unless the child class overrides the inherited methods, they retain the original functionality.

It is very important to know that PHP does not support Multiple Inheritance. However, Multi-Level Inheritance is well supported in PHP.

Basic Example

<?php

	class BaseClass {
		public function baseFunction() {
			echo 'Base Function of BaseClass Called';
		}
	}

	class ChildClass extends BaseClass {	
		public function childFunction() {
			echo 'Child Function of ChildClass Called';
		}
	}

	$childClassObject = new ChildClass();
	$childClassObject->childFunction();
	$childClassObject->baseFunction();
        /*
        Child Function of ChildClass Called
        Base Function of BaseClass Called
        */
?>

The extend keyword is used to inherit Classes in PHP. As shown in the above example, the object is the Child Class is able to access both its own function and the Base Class functions also.

More About Access Modifiers

We discussed public and private Access Modifiers in the previous article. There is one more Access Modifier that we will discuss in this article, namely protected. The protected members are not accessible outside the class, but they are inherited and accessible by the child classes.

Which Members are Inherited?

Public and Protected members are inherited. Private Members are not inherited.

Which Members are Accessible Outside the Class?

Only Public Members are Accessible outside the class. Protected Members are accessible only to Child Classes. Private Members are not accessible anywhere outside the class.

<?php

	class BaseClass {
		private $privateBaseMember = 'Private Base Member';
		protected $protectedBaseMember = 'Protected Base Member';
		public $publicBaseMember = 'Public Base Member';

		public function baseFunction() {
			echo 'Calling the Base Function';
			echo 'The Value of Private Base Member: '.$this->privateBaseMember;
			echo 'The Value of Protected Base Member: '.$this->protectedBaseMember;
			echo 'The Value of Public Base Member: '.$this->publicBaseMember;
		}
	}

	class ChildClass extends BaseClass {	
		public function childFunction() {
			echo 'Calling the Child Function';
			echo 'The Value of Protected Base Member: '.$this->protectedBaseMember;
			echo 'The Value of Public Base Member: '.$this->publicBaseMember;
		}
	}

	$childClassObject = new ChildClass();
	$childClassObject->childFunction();
	$childClassObject->baseFunction();
       
?>
Calling the Child Function
The Value of Protected Base Member: Protected Base Member
The Value of Public Base Member: Public Base Member

Calling the Base Function
The Value of Private Base Member: Private Base Member
The Value of Protected Base Member: Protected Base Member
The Value of Public Base Member: Public Base Member

The above example shows that the child class only has the access to the public and protected member of the base class. Although, the base class has access to all the members (obviously), the child class cannot access the private member. On attempting to call the private member, we will get the following error:

PHP Notice:  Undefined property: ChildClass::$privateBaseMember

Multi-Level Inheritance in PHP

PHP supports Multi-Level Inheritance. This means one class can extend another, which extends another and so on.

<?php

	class A {

	}

	class B extends A {

	}

	class C extends B {

	}
	$objectC = new C();
?>

However, PHP does not support multiple inheritance. One class cannot inherit from two parent classes.

Conclusion

In conclusion, we discussed very briefly about Inheritance in PHP. We discussed a very basic example of Inheritance in PHP. There is a lot more to discuss it in the next articles. Meanwhile, you can check out other articles on PHP on Concatly.

Kindly leave your valuable comments to help us make this resource better.

Spread the Knowledge

Leave a Reply

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