PHP Tutorial Tutorials - PHP OOP Interfaces

PHP OOP Interfaces

Interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are implemented. (the methods don't have a body )

The interface define a generic template and separates the implementation and defines the structure.

Declaring Interfaces

Interfaces can be defined with the interface keyword, and without any of the methods having their contents defined.

All methods declared in an interface must be public.

<?php

// Declare the interface 'Phone'
interface Phone
{
    public function sendSMS($to,$msg); // must be public
    public function makeACall($to);    // must be public
}

Difference between Abstract Class and Interfaces

  • A class can inherit only from one abstract class
  • A class may implement many interfaces
  • A new class extends abstract class
  • A new class implement interface

Implementing interface

All methods in the interface must be implemented within a class.
(If a class that implements an interface does not define all the methods of the interface, then it must be declared abstract)

we use the keyword implements to implement an interface:

<?php
// Implement the interface
class LauPhone implements Phone
{
    public function sendSMS($to,$msg)
    {
    }
  
    public function makeACall($to)
    {
    }
}

abstract class implementing interface

If a class that implements an interface does not define all the methods of the interface, then it must be declared abstract.

<?php
// If it is not declared `abstract`:
// Fatal error: Class WatchPhone contains 1 abstract method and must 
// therefore be declared abstract or implement the remaining methods (Phone::sendSMS)
abstract class WatchPhone implements Phone
{  
    public function makeACall($to)
    {
    }
}

the method definitions must be provided by the subclass:

class MyPhone extends WatchPhone{
     public function sendSMS($to,$msg){
        echo $to,$msg;
    }
    public function makeACall($to){
        echo $to;
    }
}
$t = new MyPhone();
$t->sendSMS("lautturi","hello");

Extending Interfaces

Interfaces can be extended like classes using the extends operator.

<?php
Interface IF1 {
    public function method1();
}
Interface IF2 extends IF1 {
    public function method2();
}

class MyClass implements IF1 {
    public function method1() {
        echo "Method1 in MyClass.\n";
    }
}
$myclass = new MyClass();
$myclass -> method1();

class MyClass2 implements IF2 {
    public function method1() {
        echo "Method1 in MyClass2.\n";
    }
    public function method2() {
        echo "Method2 in MyClass2.\n";
    }
}
// both method1 and method2 must be implemented in MyClass2.
$myclass2 = new MyClass2();
$myclass2 -> method1();
$myclass2 -> method2();
?>

implementing Multiple interface inheritance

Classes may implement multiple interfaces by separating each interface with a comma.

Notice: Prior to PHP 5.3.9, the interfaces could not have method with the same name.

There are two way to implemente multiple interfaces

  1. creata a new interface which extend the interfaces, then implemente it.
<?php
interface IF1
{
    public function method1();
}

interface IF2
{
    public function method2();
}

interface IF3 extends IF1, IF2
{
    public function method3();
}

class MyClass implements IF3
{
    public function method1()
    {
        echo "Method1.\n";
    }

    public function method2()
    {
        echo "Method2.\n";
    }

    public function method3()
    {
        echo "Method3.\n";
    }
}

$obj = new MyClass();
$obj -> method1();
$obj -> method2();
$obj -> method3();
?>
  1. creata a new class,then implemente multiple interfaces
<?php
Interface IF1 {
    public function method1();
}
Interface IF2 {
    public function method2();
}

class MyClass implements IF1, IF2 {
    public function method1() {
        echo "Method1.\n";
    }
    public function method2() {
        echo "Method2.\n";
    }
}
$obj = new MyClass();
$obj -> method1();
$obj -> method2();
?>

Extending class + Implementing interface

A class can implement interfaces while extend a class.

<?php
Interface MyInterface {
    public function method1();
}
class ParentClass {
    public function method2(){
    }
    public function method3(){
    }
}
class ChildClass extends ParentClass implements MyInterface {
    // Must implement the method1
    function method1() {
        echo "method1 in ChildClass.";
    }
    function method3() {
        echo "override method3";
    }
}

$child = new ChildClass();
$child -> method1();
?>
Date:2019-10-09 01:03:50 From:www.Lautturi.com author:Lautturi