Skip to content

Instantly share code, notes, and snippets.

@jorgecc
Created October 4, 2018 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorgecc/4a2958c71ab38ed5cc942b44d060bfb1 to your computer and use it in GitHub Desktop.
Save jorgecc/4a2958c71ab38ed5cc942b44d060bfb1 to your computer and use it in GitHub Desktop.
Example of Dependency Injection
<?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