Skip to content

Commit 6afb150

Browse files
committed
#28 - php
1 parent e6f809d commit 6afb150

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed
+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
3+
// Ejercicio basico sin LSP
4+
5+
class Bird {
6+
public function fly() {
7+
return "Puedo volar\n";
8+
}
9+
}
10+
11+
class Canary extends Bird {
12+
13+
public function name(){
14+
return "Soy un canario\n";
15+
}
16+
}
17+
18+
class Ostrich extends Bird {
19+
20+
public function name(){
21+
return "Soy un avestruz\n";
22+
}
23+
24+
public function fly() {
25+
return "No puedo volar (sobreescribi el método)\n";
26+
}
27+
}
28+
29+
echo "Sin LSP\n";
30+
$canary = new Canary();
31+
echo $canary->name();
32+
echo $canary->fly();
33+
34+
$ostrich = new Ostrich();
35+
echo $ostrich->name();
36+
echo $ostrich->fly();
37+
38+
// Vemos que hemos tenido que sobreescribir el metodo fly para el avestruz. Esto incumple el LSP
39+
40+
// Ejercicio basico con LSP
41+
42+
interface Bird2 {
43+
public function fly();
44+
}
45+
46+
class Canary2 implements Bird2 {
47+
48+
public function name(){
49+
return "Soy un canario\n";
50+
}
51+
52+
public function fly() {
53+
return "Puedo volar\n";
54+
}
55+
}
56+
57+
class Ostrich2 implements Bird2 {
58+
59+
public function name(){
60+
return "Soy un avestruz\n";
61+
}
62+
63+
public function fly() {
64+
return "No puedo volar\n";
65+
}
66+
}
67+
68+
echo "\n";
69+
echo "Con LSP\n";
70+
$canary2 = new Canary2();
71+
echo $canary2->name();
72+
echo $canary2->fly();
73+
echo "\n";
74+
$ostrich2 = new Ostrich2();
75+
echo $ostrich2->name();
76+
echo $ostrich2->fly();
77+
78+
//Al tener la interface no me ha sido necesario sobreescribir nada. Lo implemento (o no) segun mis necesidades
79+
80+
// Ejercicio Extra
81+
82+
echo "\n";
83+
echo "Ejercicio Extra\n";
84+
85+
interface Vehicle {
86+
public function speed();
87+
public function accelerate();
88+
public function brake();
89+
}
90+
91+
class Car implements Vehicle {
92+
private $speed;
93+
94+
public function __construct(){
95+
$this->speed = 0;
96+
}
97+
98+
public function speed(){
99+
return $this->speed;
100+
}
101+
102+
public function accelerate(){
103+
$this->speed += 10;
104+
}
105+
106+
public function brake(){
107+
$this->speed -= 10;
108+
}
109+
}
110+
111+
class Bicycle implements Vehicle {
112+
private $speed;
113+
114+
public function __construct(){
115+
$this->speed = 0;
116+
}
117+
118+
public function speed(){
119+
return $this->speed;
120+
}
121+
122+
public function accelerate(){
123+
$this->speed += 5;
124+
}
125+
126+
public function brake(){
127+
$this->speed -= 5;
128+
}
129+
}
130+
131+
class Truck implements Vehicle {
132+
private $speed;
133+
134+
public function __construct(){
135+
$this->speed = 0;
136+
}
137+
138+
public function speed(){
139+
return $this->speed;
140+
}
141+
142+
public function accelerate(){
143+
$this->speed += 2;
144+
}
145+
146+
public function brake(){
147+
$this->speed -= 2;
148+
}
149+
}
150+
151+
function testVehicle(Vehicle $vehicle){
152+
echo "Velocidad: ".$vehicle->speed()."\n";
153+
$vehicle->accelerate();
154+
echo "Velocidad: ".$vehicle->speed()."\n";
155+
$vehicle->accelerate();
156+
echo "Velocidad: ".$vehicle->speed()."\n";
157+
$vehicle->brake();
158+
echo "Velocidad: ".$vehicle->speed()."\n";
159+
}
160+
161+
$car = new Car();
162+
$bike = new Bicycle();
163+
$truck = new Truck();
164+
165+
echo "Vamos a probar el coche:\n";
166+
testVehicle($car);
167+
echo "Vamos a probar la bicicleta:\n";
168+
testVehicle($bike);
169+
echo "Vamos a probar el camion:\n";
170+
testVehicle($truck);

0 commit comments

Comments
 (0)