Change language

What’s the point of interfaces in PHP?

We have three different classes as follows:
class Circle {public function draw() {//} public function setRadius() {//}} class Square {public function draw() { //}} class Rectangle {public function draw() {//}} 
Now, as shown above, we have the same function in all three draw()classes and another function in the Circle class as setRadius() . We now have three different interfaces created and implemented in our classes as follows:
interface Shape {public function draw(); } interface Radius {public function setRadius(); } interface Main {public function process(); } class Circle implements Shape, Radius, Main {public function draw() {echo "Drawing Circle ..."; } public function setRadius() {echo "Setting Radius ..."; } public function process() {$this-> setRadius(); $this-> draw(); }} class Square implements Shape, Main {public function draw() {echo "Drawing Square ..."; } public function process() {$this-> draw(); }} class Rectangle implements Shape, Main {public function draw() {echo "Drawing Rectangle ..."; } public function process() {$this-> draw(); }} 
As shown above, we have implemented more than one interface in our classes. The Shapeand Maininterfaces are implemented in all classes, and the Radiusinterface is implemented only in the Circle class.Then there is the fourth class named DrawShape, soas we want to call the process()function in all classes. The process()function should call all functions in this class.
class DrawShape {public function newShape (Main $shape) {return $shape-> process(); }} 
Example : interface Shape { public function draw(); } interface Radius { public function setRadius(); } interface Main { public function process(); } class Circle implements Shape, Radius, Main { public function draw() { echo "Drawing Circle ..." ; echo "
"
; } public function setRadius() { echo "Setting Radius ..." ; } // To call all functions in this class public function process() { $this -> setRadius(); $this -> draw(); }  
} class Square implements Shape, Main { public function draw() { echo " Drawing Square ... " ; echo "
"
; } // To call all functions in this class public function process() { $this -> draw(); }  
} class Rectangle implements Shape, Main { public function draw() { echo " Drawing Rectangle ... " ; echo "
"
; } // To call all functions in this class public function process() { $this -> draw(); }  
} class DrawShape {   public function newShape (Main $shape ) { return $shape -> process(); }  
}  
// Draw a circle $shapeCircle = new Circle(); $drawCircle = new DrawShape(); $drawCircle -> newShape ( $shapeCircle );  
// Draw a square $shapeSquare = new Square(); $drawSquare = new DrawShape(); $drawSquare -> newShape ( $shapeSquare );  
// Draw rectangle-method/">rectangle $shapeRectangle = new Rectangle(); $drawSquare = new DrawShape(); $drawSquare -> newShape ( $shapeRectangle );  
?>
Output:

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method