Created
October 4, 2018 00:33
-
-
Save jorgecc/4a2958c71ab38ed5cc942b44d060bfb1 to your computer and use it in GitHub Desktop.
Example of Dependency Injection
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 ClassConvertMoneyWithInjection | |
{ | |
/** @var IConversor */ | |
public $service; | |
public function setService(IConversor $srv) { | |
$this->service=$srv; | |
} | |
public function example($money) { | |
echo "$money is converted in ".$this->service->conversor($money)."<br>"; | |
} | |
} | |
interface IConversor { | |
function conversor($value); | |
} | |
class ServiceEuroToDollar implements IConversor { | |
function conversor($value) | |
{ | |
return $value*1.15; | |
} | |
} | |
class ServiceDollarToEuro implements IConversor { | |
function conversor($value) | |
{ | |
return $value/1.15; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment