Created
December 15, 2013 23:36
-
-
Save azcdev/7979893 to your computer and use it in GitHub Desktop.
Inyeccion de dependencias en PHP. Los objetos "carro" comparten el mismo motor, por lo cual el motor solamente se puede encender una vez
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 | |
class Motor { | |
private $prendido; | |
public function __construct() {} | |
public function arranca() { | |
if(!$this->prendido) { | |
echo "run run run run..."; | |
$this->prendido = true; | |
} else { | |
throw new Exception("Ehh!! Ya estoy prendido me vas a descomponer!!"); | |
} | |
} | |
} | |
class MotorDiesel extends Motor { | |
public function __construct() {} | |
} | |
class Carro { | |
private $motor; | |
public function __construct(Motor $motor) { | |
$this->motor = $motor; | |
$this->motor->arranca(); | |
} | |
} | |
$motorDiesel = new MotorDiesel(); | |
$motorGasolina = new Motor(); | |
$sedan = new Carro($motorGasolina); | |
$camioneta = new Carro($motorGasolina); | |
$autobus = new Carro($motorDiesel); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment