-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsp3.php
62 lines (51 loc) · 1.75 KB
/
lsp3.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
<?php
class Animal
{
protected string $name;
public function __construct($name)
{
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
public function eat(): void
{
echo $this->name . " is eating.\n";
}
}
class Dog extends Animal
{
public function bark()
{
echo $this->name . " is barking.\n";
}
}
class Cat extends Animal
{
public function meow()
{
echo $this->name . " is meowing.\n";
}
}
// Let's create some instances of the classes.
$animal = new Animal("Generic Animal");
$dog = new Dog("Fido");
$cat = new Cat("Whiskers");
// Now, let's try calling the getName method and eat method on each instance.
echo $animal->getName() . "\n"; // Outputs "Generic Animal"
echo $dog->getName() . "\n"; // Outputs "Fido"
echo $cat->getName() . "\n"; // Outputs "Whiskers"
$animal->eat(); // Outputs "Generic Animal is eating."
$dog->eat(); // Outputs "Fido is eating."
$cat->eat(); // Outputs "Whiskers is eating."
// This is where we see the Liskov Substitution Principle in action.
//$dog->meow(); // This will generate a fatal error, because the Dog class doesn't have a meow method.
//$cat->bark(); // This will also generate a fatal error, because the Cat class doesn't have a bark method.
// The Dog and Cat classes are still substitutable for their parent Animal class,
// because they both have a getName method and an eat method that behaves as expected.
// However, they have additional methods (bark and meow, respectively) that are specific to their class
// and not present in the parent Animal class.
// So if we try to call these methods on instances of the Animal class (or any other subclass of Animal),
// it will result in a fatal error.