Interfaces and Instanceof in OOP in PHP
Using instanceof
, you can check
whether a certain class implements a given interface
or not. Let's look at an example. Suppose we
have the following class:
<?php
class Quadrate implements iFigure
{
}
?>
Let's create an object of this class and check it
with the instanceof
operator:
<?php
$quadrate = new Quadrate;
var_dump($quadrate instanceof Quadrate); // will output true
var_dump($quadrate instanceof Figure); // will output true
?>
Create an interface Figure3d
(three-dimensional
figure), which will have the method getVolume
(get volume) and the method getSurfaceSquare
(get surface area).
Create a class Cube
that will
implement the interface Figure3d
.
Create several objects of the class Quadrate
,
several objects of the class Rectangle
and several objects of the class Cube
.
Write them into the array $arr
in random
order.
Loop through the array $arr
and output
to the screen only the areas of objects implementing
the interface iFigure
.
Loop through the array $arr
and output
the areas for flat figures, and for three-dimensional
ones - their surface areas.