# INHERITANCE-INTERFACE

Inheritance :

PHP class definitions can optionally inherit from a parent class definition by using the extends clause. The syntax is as follows:

class Child extends Parent {

<definition body>

}

The effect of inheritance is that the child class (or subclass or derived class) has the following characteristics:

- Automatically has all the member variable declarations of the parent class.
- Automatically has all the same member functions as the parent, which (by default) will work the same way as those functions do in the parent.

<?php

class parent1

{

var $name=”test”;

var $phone=”123456″;

public function disp()

{

echo $this->name;

echo “<br>”;

echo $this->phone;

echo “<br>”;

}

}

class inheritance1 extends parent1

{

function read()

{

echo “it is working fine”;

}

}

$obj=new inheritance1();

$obj->disp();

$obj->read();

?>

## Interfaces :

##

Interfaces are defined to provide a common function names to the implementors. Different implementors can implement those interfaces according to theri requirements. You can say, interfaces are skeltons which are implemented by developers.

As of PHP5, it is possible to define an interface, like this :

interface Mail { public function sendMail();}

Then, if another class implemented that interface, like this :

class Report implements Mail { // sendMail() Definition goes here}

Example :

<?php

interface test

{

function disp();

}

class hello implements test

{

function disp()

{

echo “it is interface function”;

}

}

$obj=new hello();

$obj->disp();

?>

## Abstract Classes :

An abstract class is one that cannot be instantiated, only inherited. You declare an abstract class with the keyword abstract, like this :

When inheriting from an abstract class, all methods marked abstract in the parent’s class declaration must be defined by the child; additionally, these methods must be defined with the same visibillity.

abstract class MyAbstractClass { abstract function myAbstractFunction() { }}

Example :

<?php

abstract class Animal

{

public $name;

public $age;

public function Describe()

{

return $this->name . “, ” . $this->age . ” years old”;

}

abstract public function Greet();

}

class Dog extends Animal

{

public function Greet()

{

return “Woof!”;

}

}

$animal = new Dog();

$animal->name = “Bob”;

$animal->age = 7;

echo $animal->Describe();

echo $animal->Greet();

?>

## Static Keyword :

##

Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can).

Example :

<?php

Class staticexample

{

public static $name=”om”; // static variable

public static function disp() //static member function

{

echo “static member function is this”;

}

}

$obj=new staticexample();

print staticexample::$name; // accessing static variable

echo “<br>”;

staticexample::disp(); //accessing static member function

?>

## Final Keyword :

PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.

Following example results in Fatal error: Cannot override final method BaseClass::moreTesting()

<?phpclass BaseClass { public function test() { echo “BaseClass::test() called<br>”; } final public function moreTesting() { echo “BaseClass::moreTesting() called<br>”; }} class ChildClass extends BaseClass { public function moreTesting() { echo “ChildClass::moreTesting() called<br>”; }}?>

Parent keyword :

<?php

class A

{

function example()

{

echo “I am A::example() and provide basic functionality.<br />\n”;

}

}

class B extends A {

function example() {

echo “I am B::example() and provide additional functionality.<br />\n”;

parent::example();

}

}

$b =new B;

// This will call B::example(), which will in turn call A::example().

$b->example();

?>