pTutorial

New way to learn PHP

PHP 5 Interface

PHP Interface

Interfaces works same as abstract classes, in the interface they declare a consistent set of methods that classes must implement. In the case of interface parent-child relationship does not exist. A class can access the properties of the interface with the help of implements keyword.

Note: At the same time you can extend class also.

You create an interface much like a class, except that you use the keyword interface instead of the class keyword. Then specify a list of methods that implementing classes must include all the method.

Suppose an interface has two methods, and another class that implements the interface must have given the body.

Syntax of PHP interface

<?php

interface NameOfInterface 
{
Name of method with the number of arguments
……………………….
……………………….
}

?>

Example of interface

<?php

interface MyFirstInterface
{
public function FirstMethod( $name );
public function SecondMethod( $name, $roll );
}

?>

Some point about interface

1. You cannot contain properties, only the methods declaration allowed in case of interface as shown above.

2. All method must be public in case of interface.

3. You can then make a class that implements an interface using the implements keyword.

PHP Class with interface

<?php

Class FirstClass implements MyFirstInterface 
{
public function FirstMethod ( $name )
{
//Method implementation here
}
public function SecondMethod ( $name, $roll ) 
{
//Method implementation here
}
}


?>

You can keep blank method as shown above but must be there, you cannot leave any of interface's method.

Two interface implementation

4. You can implement more than one interfaces with the help of comma.

<?php

class FirstClass implements MyFIrstInterface, MySecondInterface
{
// all Methods implementation from MyFIrstInterface 
// and MySecondInterface.
}

?>

PHP Interface example

<?php

interface MyInterface 
{
public function myMethod( $name);
}


class MyFirstClass implements MyInterface
{
public function myMethod( $name1)
{
echo "$name1";
}

}
$firstObj = new MyFirstClass;
$firstObj->myMethod("John");
$firstObj->myMethod("
Brown"); ?>

Output

John
Brown

In the above example, MyInterface is an interface that have one method and one class named MyFirstClass implements the interface give the body of the method.

PHP Two Interface Example

<?php


interface MyInterface 
{
public function myMethod( $name);
}

interface MySecondInterface 
{
public function mySecondMethod( $name);
}

class MyFirstClass implements MyInterface,MySecondInterface
{
public function myMethod( $name1)
{
echo "$name1";
}


public function mySecondMethod( $name1)

{
echo "$name1";
}

}
$firstObj = new MyFirstClass;
$firstObj->myMethod("John");
$firstObj->mySecondMethod("\tBrown");

?>

You can simply implements two interfaces by using comma (,) as shown above.

Output

John Brown

You can also create an interface in separate PHP file and include interface with the help of include keyword and use the functionality of an interface as shown below.

PHP interface with include

myinterface.php
<?php

interface MyFirstInterface
{
public function FirstMethod( $name );

}

?>

<?php

include 'myinterface.php';
class MyFirstClass implements MyFirstInterface
{
public function myMethod( $name1)
{
echo "$name1";
}

}
$firstObj = new MyFirstClass;
$firstObj->myMethod("John");

?>

Output

John

Suppose you have a interface named "MyInterface" that have two methods and at a time of implementation you want only one, you cannot leave, so keep this method without body as shown below.

PHP interface with one method

myinterface.php
<?php

interface MyFirstInterface
{
public function myMethod( $name);
public function myMethod1( $name);
}

?>

<?php

include 'myinterface.php';
class MyFirstClass implements MyFirstInterface
{
public function myMethod( $name1)
{
echo "$name1";
}

public function myMethod1( $name1)
{
}

}
$firstObj = new MyFirstClass;
$firstObj->myMethod("John");

?>

In the above example, we are not using myMethod1 but still write with the blank body.

Output

John

PHP 5 Multiple interface

You can also extends mutiple interface using extends keyword in PHP.

<?php

interface first
{
public function set();
}

interface second extends first
{
public function display();
}

class Computer implements second
{
public function set()
{
echo "First interface method
"; } public function display() { echo "Second interface method"; } } $obj=new Computer(); $obj->set(); $obj->display(); ?>

Output

First interface method
Second interface method

You extends interface like a class with the help of extends keyword. First and second are the interfaces and Computer is the class which implements the second interface, so that all the method of the first and the second interface must be implemented in Computer class.


QUOTES OF THE DAY

We are never deceived we deceive ourselves.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER