-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsp.php
63 lines (55 loc) · 1.67 KB
/
lsp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
/**
* In this example, we have a Rectangle class that has setWidth(), setHeight(), and area() methods.
* We also have a Square class that extends the Rectangle class, and overrides the setWidth() and setHeight() methods
* to ensure that the width and height are always equal.
*
* The printArea() function takes a Rectangle object as a parameter and calculates its area by setting its width and height
* and calling the area() method. Both the Rectangle and Square objects can be passed to this function,
* since the Square class inherits from the Rectangle class and is substitutable for it.
*
* This adheres to the Liskov Substitution Principle, as the Square class can be used wherever a Rectangle object is
* expected, without causing any unexpected behavior or violating the behavior of the Rectangle class.
*
*
*/
class Rectangle
{
protected $width;
protected $height;
public function setWidth($width)
{
$this->width = $width;
}
public function setHeight($height)
{
$this->height = $height;
}
public function area()
{
return $this->width * $this->height;
}
}
class Square extends Rectangle
{
public function setWidth($width)
{
$this->width = $width;
$this->height = $width;
}
public function setHeight($height)
{
$this->width = $height;
$this->height = $height;
}
}
function printArea(Rectangle $rectangle)
{
$rectangle->setWidth(4);
$rectangle->setHeight(5);
echo "Area: " . $rectangle->area() . "<br>";
}
$rectangle = new Rectangle();
$square = new Square();
printArea($rectangle); // prints "Area: 20"
printArea($square); // prints "Area: 20"