PHP Inheritance Class


 

PHP Inheritance Class

Inheritance is a property of Object Oriented Programming, PHP also supports this principle in its programming as object model. With the help of this property classes and objects get more flexibility, scalability in programming

Inheritance is a property of Object Oriented Programming, PHP also supports this principle in its programming as object model. With the help of this property classes and objects get more flexibility, scalability in programming

Inheritance in PHP:

Inheritance is a property of Object Oriented Programming, PHP also supports this principle in its programming as object model. With the help of this property classes and objects  get more flexibility, scalability in programming

At the same time we must keep in our mind that when we should not use the inheritance. We must not use inheritance when we have only different values in classes, we must use the inheritance when the structure and the behavior are different. It is always advisable that we must give preference to object composition over inheritance.

 In the following example we will come to know how a class could be declared as a child class of another class using extends keyword.

PHP Inheritance Example:

<?php

class One

{

public function printItem($string)

{

echo 'This argument is passing from '.$string.'<br/>';

}

}

class Two extends One

{

}

$baseObj=new One();

$childObj=new Two();

$baseObj->printItem("Base");

$childObj->printItem("Child");

?>

 

Output:

This argument is passing from Base
This argument is passing from Child

Ads