PHP Interface Class


 

PHP Interface Class

In this tutorial you will study about interface of PHP, how to declare and implement an interface, what points should be kept in mind when we declare an interface etc. Interface is also a class but it has few constraints unlike any other ordinary concrete class and it supports the multiple inheritance.

In this tutorial you will study about interface of PHP, how to declare and implement an interface, what points should be kept in mind when we declare an interface etc. Interface is also a class but it has few constraints unlike any other ordinary concrete class and it supports the multiple inheritance.

PHP Interface Class:

PHP does not support multiple inheritance directly, to implement this we need Interface. It is much similar to Interface of Java.

In PHP, signature of the method are declared in the Interface body, and the body part of the method is implemented in derived class. Variables are declared as constant and it can not be changed in the child classes.

We use implement keyword to extend this kind of class, at the same time we can implement more than one interface and one interface can be implemented by another interface.

All methods declared in an interface must be public and the variables should be constant.

This is mandatory that we must declare the body part of the method in the derived class otherwise an error message will be generated.

Example:

<?php

interface Inter{

const a="This is constant value";

public function disp(); }

class A implements Inter{

function show(){

echo self::a."<br/>";}

public function disp(){

echo "Inside the disp function";}}

$a=new A();

$a->show();

$a->disp();

?>

Output:

This is constant value
Inside the disp function

 

Ads