PHP Object Inheritance


Introduction

Inheritance is an important principle of object oriented programming methodology. Using this principle, relation between two classes can be defined. PHP supports inheritance in its object model.

PHP uses extends keyword to establish relationship between two classes.

Syntax

class B extends A

where A is the base class (also called parent called) and B is called a subclass or child class. Child class inherits public and protected methods of parent class. Child class may redefine or override any of inherited methods. If not, inherited methods will retain their functionality as defined in parent class, when used with object of child class.

Definition of parent class must precede child class definition. In this case, definition of A class should appear before definition of class B in the script.

Example

<?php
class A{
   //properties, constants and methods of class A
}
class B extends A{
   //public and protected methods inherited
}
?>

If autoloading is enabled, definition of parent class is obtained by loading the class script.

Inheritance Example

Following code shows that child class inherits public and protected members of parent class

Example

 Live Demo

<?php
class parentclass{
   public function publicmethod(){
      echo "This is public method of parent class
" ;    }    protected function protectedmethod(){       echo "This is protected method of parent class
" ;    }    private function privatemethod(){       echo "This is private method of parent class
" ;    } } class childclass extends parentclass{    public function childmethod(){       $this->protectedmethod();       //$this->privatemethod(); //this will produce error    } } $obj=new childclass(); $obj->publicmethod(); $obj->childmethod(); ?>

Output

This will produce following result. −

This is public method of parent class
This is protected method of parent class
PHP Fatal error: Uncaught Error: Call to private method parentclass::privatemethod() from context 'childclass'

Method Overriding Example

If a method inherited from parent class is redefined in child class, new definition overrides earlier functionality. In following example, publicmethod is defined again in child class

Example

 Live Demo

<?php
class parentclass{
public function publicmethod(){
   echo "This is public method of parent class
" ; } protected function protectedmethod(){    echo "This is protected method of parent class
" ; } private function privatemethod(){    echo "This is private method of parent class
" ; } } class childclass extends parentclass{    public function publicmethod(){       echo "public method of parent class is overridden in child class
" ;    } } $obj=new childclass(); $obj->publicmethod(); ?>

Output

This will produce following result. −

public method of parent class is overridden in child class

Heirarchical Inheritance

PHP doesn't support multiple inheritance. Hence a class can not extend two or more classes. However, it supports heirarchical inheritance as follows:

Example

 Live Demo

<?php
class A{
   function test(){
      echo "method in A class";
   }
}
class B extends A{
   //
}
class C extends B{
   //
}
$obj=new C();
$obj->test();
?>

Output

This will show following result

method in A class

Updated on: 18-Sep-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements