In programming, we always try not to repeat code again and again and separating implementation from interface for security proposes. Inheritance is new option to do these things more effectively. It is just like real life. Your father has taken some property from your grandfather and he has also modify it. Same you will take some property from your father and you will also modify it and will also add some other property to it. There is such a relationship in classes also. It is called inheritance. Any class can extend other classes and can achieve their public and protected (not private) functions, variables, objects and other instances.
- <?php
-
- /**
- * inheritance in php
- */
- class Grandfather
- {
- // global varible
- var $global_variable;
-
- // constructor of grandfather
- function __construct()
- {
- $this->global_variable = 56;
- echo "I am grandfather
- ";
- }
-
- function default_function()
- {
- echo "this is default function in grandfather
- ";
- }
-
- private function private_function()
- {
- echo "this is private function in grandfather
- ";
- }
-
- protected function protected_function()
- {
- echo "this is protected function in grandfather
- ";
- }
-
- public function public_function()
- {
- echo "this is public function in grandfather
- ";
- }
- }
-
- /**
- * this is sub class which is extending Grandfather class
- * this will inherit all property except private property
- */
- class Father extends Grandfather
- {
- // global variable
- var $father_var;
- function __construct()
- {
- // equivalent to => parent::__condtruct();
- Grandfather::__construct();
- $this->father_var = 256;
- echo "I am father
- ";
- }
-
- public function display_all()
- {
- $this->default_function();
- $this->protected_function();
- $this->public_function();
- echo "I am father's display_all
- ";
- parent::public_function();
- }
- }
-
- /**
- * this is child class which is inheriting from father
- * this is indirectly also inheriting from grandfather
- * just like you because father's property includes
- * grandfather's property
- */
- class Child extends Father
- {
- // constructor in Child
- function __construct()
- {
- Grandfather::__construct();
- echo "I am child
- ";
- }
-
- // child is modifying father's funciton
- // it is called overriding
- function display_all()
- {
- echo "function from father
- ";
- // including father's display function
- parent::display_all();
- echo "new added in child
- ";
- }
- }
- $obj = new Father();
- $obj->display_all();
-
- echo "
-
-
- Child object call
-
- ";
- $obj2 = new Child();
- $obj2->display_all();
-
- ?>
Output:
- I am grandfather
- I am father
- this is default function in grandfather
- this is protected function in grandfather
- this is public function in grandfather
- I am father's display_all
- this is public function in grandfather
- Child object call
-
- I am grandfather
- I am child
- function from father
- this is default function in grandfather
- this is protected function in grandfather
- this is public function in grandfather
- I am father's display_all
- this is public function in grandfather
- new added in child
It is a complete image of the inheritance. Lets understand it:
- Child is inheriting from Father, who is inheriting from Grandfather. It is called Multilevel inheritance.
- Sub class (class which is inheriting) can access all non-private properties of super class.
- Sub class can call functions of super class by using parent::function_name() (for one level up, i.e., child can use it for father and father can use it for grandfather but child can not use it for grandfather. Example, line 61 and 86) or SuperClass_name::function_name() (also child can do it for calling functions of grandfather like in line 76).
- Sub class can modify the functions of the super class. It is known as overriding.
- PHP does not support multiple inheritance, i.e., one class can not inherit from more than one class.