Thursday 3 June 2021

Php: class implement multiple interfaces

In Php, a class can implement multiple interfaces.

 

Syntax

class ClassName implements interface1, interface2....interfaceN{

    //Provide definition to all the method signatures of interface.

}

 

Example

class MyCircle implements Circle{
    public const PI = 3.14;

    public function area_of_circle($radius){
        return self::PI * $radius * $radius;
    }
    public function perimeter_of_circle($radius){
        return 2 * self::PI * $radius;
    }

    public function info($msg){
        echo $msg;
    }

    public function debug($msg){
        $this->info($msg);
    }

    public function error($msg){
        $this->info($msg);
    }

}

 

Class implementing the interfaces must provide definition of all the methods signatures.

 

class_implement_multiple_interfaces.php

#!/usr/bin/php

<?php

interface Circle{
    public function area_of_circle($radius);
    public function perimeter_of_circle($radius);
}

interface Logger{
    public function info($msg);
    public function debug($msg);
    public function error($msg);
}

class MyCircle implements Circle{
    public const PI = 3.14;

    public function area_of_circle($radius){
        return self::PI * $radius * $radius;
    }
    public function perimeter_of_circle($radius){
        return 2 * self::PI * $radius;
    }

    public function info($msg){
        echo $msg;
    }

    public function debug($msg){
        $this->info($msg);
    }

    public function error($msg){
        $this->info($msg);
    }

}

$circle1 = new MyCircle();
$radius = 12;

$area = $circle1->area_of_circle($radius);
$perimeter = $circle1->perimeter_of_circle($radius);

echo "Radius of Circle : $radius\n";
echo "Area of Circle : $area\n";
echo "Perimeter of Circle : $perimeter\n";

$circle1->debug("\nI am debug message\n");
$circle1->info("I am information message\n");
$circle1->error("I am error message\n");
?>

 

Output

$./class_implement_multiple_interfaces.php 

Radius of Circle : 12
Area of Circle : 452.16
Perimeter of Circle : 75.36

I am debug message
I am information message
I am error message

 

 


  

Previous                                                    Next                                                    Home

No comments:

Post a Comment