Skip to content

Instantly share code, notes, and snippets.

@Djuki
Last active December 9, 2015 23:48
Show Gist options
  • Save Djuki/4346253 to your computer and use it in GitHub Desktop.
Save Djuki/4346253 to your computer and use it in GitHub Desktop.
Primer korišćenja IoC containera
<?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