Last active
December 9, 2015 23:48
-
-
Save Djuki/4346253 to your computer and use it in GitHub Desktop.
Primer korišćenja IoC containera
This file contains 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 | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
use Symfony\Component\DependencyInjection\Reference; | |
class Engine | |
{ | |
private $cubic; | |
/** | |
* Private construct | |
* @param int $cubic | |
*/ | |
private function __construct($cubic) | |
{ | |
$this->cubic = $cubic; | |
} | |
/** | |
* Factory method | |
* @param int $cubic | |
*/ | |
static public function factory($cubic) | |
{ | |
return new self($cubic); | |
} | |
} | |
$container = new ContainerBuilder(); | |
$container->setParameter('engine.class', '\\Model\\Engine'); | |
$container->register('my.tractor', '\\Model\\Tractor') | |
->addMethodCall('setEngine', array(new Reference('my.engine'))); | |
$container->setDefinition('my.engine', new Definition('%engine.class%')) | |
->addArgument(1800) | |
->setFactoryClass('%engine.class%') | |
->setFactoryMethod('factory'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment