Interfaces in PHP
class A
{
public function printItem($string)
{
echo ' Hi : ' . $string;
}
public function printPHP()
{
echo 'I am from valuebound' . PHP_EOL;
}
}
class B extends A
{
public function printItem($string)
{
echo 'Hi: ' . $string . PHP_EOL;
}
public function printPHP()
{
echo "I am from ABC";
}
}
$a = new A();
$b = new B();
$a->printItem('Raju');
$a->printPHP();
$b->printItem('savan');
$b->printPHP();
?>
Output
Hi : Pavan
I am from valuebound
Hi: savan
I am from ABC
Multilevel Inheritance
class A
{
public function myage()
{
return ' age is 80';
}
}
class B extends A
{
public function mysonage()
{
return ' age is 50';
}
}
class C extends B
{
public function mygrandsonage()
{
return 'age is 20';
}
public function myHistory()
{
echo "Class A " .$this->myage();
echo "Class B ".$this-> mysonage();
echo "Class C " . $this->mygrandsonage();
}
}
$obj = new C();
$obj->myHistory();
?>
Output
Class A is 80
Class B is 50
Class C 20
Interfaces are defined to provide a common function names to the implemented. Different implements can implement those interfaces according to their requirements. You can say, interfaces are skeletons which are implemented by developers. As of PHP5, it is possible to define an interface, like this –
Types of Inheritance- Single Level Inheritance
- Multilevel Inheritance
Single Level Inheritance: In Single Level Inheritance the Parent class methods will be extended by the child class. All the methods can be inherited.
Eg:-<?phpclass A
{
public function printItem($string)
{
echo ' Hi : ' . $string;
}
public function printPHP()
{
echo 'I am from valuebound' . PHP_EOL;
}
}
class B extends A
{
public function printItem($string)
{
echo 'Hi: ' . $string . PHP_EOL;
}
public function printPHP()
{
echo "I am from ABC";
}
}
$a = new A();
$b = new B();
$a->printItem('Raju');
$a->printPHP();
$b->printItem('savan');
$b->printPHP();
?>
Output
Hi : Pavan
I am from valuebound
Hi: savan
I am from ABC
Multilevel Inheritance
In MultiLevel Inheritance, the parent class method will be inherited by child class and again subclass will inherit the child class method.
Eg:- <?phpclass A
{
public function myage()
{
return ' age is 80';
}
}
class B extends A
{
public function mysonage()
{
return ' age is 50';
}
}
class C extends B
{
public function mygrandsonage()
{
return 'age is 20';
}
public function myHistory()
{
echo "Class A " .$this->myage();
echo "Class B ".$this-> mysonage();
echo "Class C " . $this->mygrandsonage();
}
}
$obj = new C();
$obj->myHistory();
?>
Output
Class A is 80
Class B is 50
Class C 20
0 comments
Post a Comment