Interfaces and Traits
Interface Shape
Different classes can share a method shape by implementing the same interface.
shared shape
The caller only needs the methods promised by the interface.
Interface Shape
interface_shape.php
Replay: real traced execution (multi-file project)
<?php
interface Measurable
{
public function measure(): int;
}
class Rectangle implements Measurable
{
public function __construct(private int $width, private int $height)
{
}
public function measure(): int
{
return $this->width * $this->height;
}
}
$width = 4;
$shape = new Rectangle($width, 3);
$area = $shape->measure();
echo "width=" . $width . "\n";
echo "area=" . $area . "\n";
<?php
interface Measurable
{
public function measure(): int;
}
class Rectangle implements Measurable
{
public function __construct(private int $width, private int $height)
{
}
public function measure(): int
{
return $this->width * $this->height;
}
}
$width = 2;
$shape = new Rectangle($width, 3);
$area = $shape->measure();
echo "width=" . $width . "\n";
echo "area=" . $area . "\n";
<?php
interface Measurable
{
public function measure(): int;
}
class Rectangle implements Measurable
{
public function __construct(private int $width, private int $height)
{
}
public function measure(): int
{
return $this->width * $this->height;
}
}
$width = 6;
$shape = new Rectangle($width, 3);
$area = $shape->measure();
echo "width=" . $width . "\n";
echo "area=" . $area . "\n";
$width ← 4
19$width→ 4 = 4; //@width=2, 620$shape = new Rectangle($width4, 3);21$area = $shape->measure();public function __construct(private int $width, private int $height)
8{9 public function __construct(private int $width4, private int $height3)10 {11 }$shape ← Rectangle
19$width = 4; //@width=2, 620$shape→ Rectangle = new Rectangle($width4, 3);21$area = $shapeRectangle->measure();public function measure(): int
13public function measure(): int14{15 return $this->width4 * $this->height3;16}$area ← 12
20$shape = new Rectangle($width, 3);21$area→ 12 = $shapeRectangle->measure();2223echo "width=" . $width4 . "\n";24echo "area=" . $area12 . "\n";outputwidth=4 area=12
$width ← 2
19$width→ 2 = 2;20$shape = new Rectangle($width2, 3);21$area = $shape->measure();public function __construct(private int $width, private int $height)
8{9 public function __construct(private int $width2, private int $height3)10 {11 }$shape ← Rectangle
19$width = 2;20$shape→ Rectangle = new Rectangle($width2, 3);21$area = $shapeRectangle->measure();public function measure(): int
13public function measure(): int14{15 return $this->width2 * $this->height3;16}$area ← 6
20$shape = new Rectangle($width, 3);21$area→ 6 = $shapeRectangle->measure();2223echo "width=" . $width2 . "\n";24echo "area=" . $area6 . "\n";outputwidth=2 area=6
$width ← 6
19$width→ 6 = 6;20$shape = new Rectangle($width6, 3);21$area = $shape->measure();public function __construct(private int $width, private int $height)
8{9 public function __construct(private int $width6, private int $height3)10 {11 }$shape ← Rectangle
19$width = 6;20$shape→ Rectangle = new Rectangle($width6, 3);21$area = $shapeRectangle->measure();public function measure(): int
13public function measure(): int14{15 return $this->width6 * $this->height3;16}$area ← 18
20$shape = new Rectangle($width, 3);21$area→ 18 = $shapeRectangle->measure();2223echo "width=" . $width6 . "\n";24echo "area=" . $area18 . "\n";outputwidth=6 area=18