Object Inheritance in PHP

Inheritance is a well-established programming principle, and PHP makes use of this principle in its object model. It will affect the way many classes and objects relate to one another.

For example, when you extend a PHP class, the subclass inherits all of the public and protected methods from the parent class. Unless a class overrides those methods, they will retain their original functionality.

Inheritance
<?php
class ParentClass
{
public function printItem($string)
{
echo ‘ParentClass: ‘ . $string.'< br/ >’;
}
public function printPHP()
{
echo ‘PHP is great.< br/ >’;
}
}

class SubClass extends ParentClass
{

public function printItem($string)
{
echo ‘SubClass: ‘ . $string.'< br/ >’;
}
}

$a = new ParentClass();
$b = new SubClass();
$a->printItem(‘Test01’); // ParentClass: Test01
$a->printPHP(); // PHP is great.
$b->printItem(‘Test02’); // SubClass: Test02
$b->printPHP(); // PHP is great.

?>

OUTPUT :
ParentClass: Test01
PHP is great.
SubClass: Test02
PHP is great.