Created
November 9, 2016 12:58
-
-
Save gorkamu/d312262a1d38caf4b0da251a31ec19cf to your computer and use it in GitHub Desktop.
Ejemplo de polimorfismo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class Animal | |
{ | |
abstract public function moverse(); | |
} | |
class Gorrino extends Animal | |
{ | |
public function moverse() { | |
echo "El gorrino corre".PHP_EOL; | |
} | |
} | |
class Pajarico extends Animal | |
{ | |
public function moverse() { | |
echo "El pajarico vuela".PHP_EOL; | |
} | |
} | |
class Accion | |
{ | |
public function andar(Animal $animal) { | |
$animal->moverse(); | |
} | |
} | |
$animales = [new Gorrino(), new Pajarico()]; | |
$accion = new Accion(); | |
foreach($animales as $animal) { | |
$accion->andar($animal); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment