Share on Google+Share on Google+

PHP Inheritance Class

Advertisement
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

Advertisements

Posted on: February 19, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.