Skip to content

Instantly share code, notes, and snippets.

@adililhan
Created August 1, 2013 09:30
Show Gist options
  • Save adililhan/6129883 to your computer and use it in GitHub Desktop.
Save adililhan/6129883 to your computer and use it in GitHub Desktop.
Symfony'nin dependency injection componentinin kullanımını göstermek amacıyla hazırlanmış örnek sınıf.
<?php
/**
* Looking for tests DependencyInjectionTest.php
*/
require_once 'vendor/autoload.php';
interface iCurrency {
public function getExchangeRate();
}
interface iObject{
public function setInstance(iCurrency $currency);
public function getValue();
}
class Dollar implements iCurrency {
public function getExchangeRate() {
return '1.93';
}
}
class Euro implements iCurrency {
public function getExchangeRate() {
return '1.57';
}
}
class Money implements iObject {
public function setInstance(iCurrency $currency) {
$this->_currency = $currency;
}
public function getValue() {
return $this->_currency->getExchangeRate();
}
}
class Controller {
public function setObject(iCurrency $instanceObject, iObject $object) {
$this->_object = $object;
$this->_object->setInstance($instanceObject);
}
public function getTLtoCurrency($turkishLira) {
if( !is_float($turkishLira)) {
throw new InvalidArgumentException("This value is not float.");
}
$TLtoCurrency = $turkishLira * $this->_object->getValue();
return $TLtoCurrency;
}
}
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
$cont = new ContainerBuilder;
$cont->register('dollar', 'Dollar');
$cont->register('money', 'Money');
$cont->register('control', 'Controller')
->addMethodCall('setObject', array(new Reference('dollar'), new Reference('money')));
try {
$convertedTL = $cont->get('control')->getTLtoCurrency(2.00);
echo $convertedTL;
} catch(InvalidArgumentException $e) {
echo $e->getMessage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment