Inheritance in PHP

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.

  1. <?php
  2. /**
  3. * inheritance in php
  4. */
  5. class Grandfather
  6. {
  7. // global varible
  8. var $global_variable;
  9. // constructor of grandfather
  10. function __construct()
  11. {
  12. $this->global_variable = 56;
  13. echo "I am grandfather
  14. ";
  15. }
  16. function default_function()
  17. {
  18. echo "this is default function in grandfather
  19. ";
  20. }
  21. private function private_function()
  22. {
  23. echo "this is private function in grandfather
  24. ";
  25. }
  26. protected function protected_function()
  27. {
  28. echo "this is protected function in grandfather
  29. ";
  30. }
  31. public function public_function()
  32. {
  33. echo "this is public function in grandfather
  34. ";
  35. }
  36. }
  37. /**
  38. * this is sub class which is extending Grandfather class
  39. * this will inherit all property except private property
  40. */
  41. class Father extends Grandfather
  42. {
  43. // global variable
  44. var $father_var;
  45. function __construct()
  46. {
  47. // equivalent to => parent::__condtruct();
  48. Grandfather::__construct();
  49. $this->father_var = 256;
  50. echo "I am father
  51. ";
  52. }
  53. public function display_all()
  54. {
  55. $this->default_function();
  56. $this->protected_function();
  57. $this->public_function();
  58. echo "I am father's display_all
  59. ";
  60. parent::public_function();
  61. }
  62. }
  63. /**
  64. * this is child class which is inheriting from father
  65. * this is indirectly also inheriting from grandfather
  66. * just like you because father's property includes
  67. * grandfather's property
  68. */
  69. class Child extends Father
  70. {
  71. // constructor in Child
  72. function __construct()
  73. {
  74. Grandfather::__construct();
  75. echo "I am child
  76. ";
  77. }
  78. // child is modifying father's funciton
  79. // it is called overriding
  80. function display_all()
  81. {
  82. echo "function from father
  83. ";
  84. // including father's display function
  85. parent::display_all();
  86. echo "new added in child
  87. ";
  88. }
  89. }
  90. $obj = new Father();
  91. $obj->display_all();
  92. echo "
  93. Child object call
  94. ";
  95. $obj2 = new Child();
  96. $obj2->display_all();
  97. ?>

Output:

  1. I am grandfather
  2. I am father
  3. this is default function in grandfather
  4. this is protected function in grandfather
  5. this is public function in grandfather
  6. I am father's display_all
  7. this is public function in grandfather
  8. Child object call
  9. I am grandfather
  10. I am child
  11. function from father
  12. this is default function in grandfather
  13. this is protected function in grandfather
  14. this is public function in grandfather
  15. I am father's display_all
  16. this is public function in grandfather
  17. new added in child

It is a complete image of the inheritance. Lets understand it:

  1. Child is inheriting from Father, who is inheriting from Grandfather. It is called Multilevel inheritance.
  2. Sub class (class which is inheriting) can access all non-private properties of super class.
  3. 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).
  4. Sub class can modify the functions of the super class. It is known as overriding.
  5. PHP does not support multiple inheritance, i.e., one class can not inherit from more than one class.


ReactJS with Redux Online Training by Edureka

About Harish Kumar

Harish, a fullstack developer at www.lyflink.com with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and non-technical topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Complete Python Bootcamp: Go from zero to hero in Python 3

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites

Subscribe

Subscribe now and get weekly updates directly in your inbox!