Previous Next

Using Multiple Definitions From Multiple Sources

In all actuality, you will be using code from multiple places, some Zend Framework code, some other 3rd party code, and of course, your own code that makes up your application. Here is a method for consuming definitions from multiple places:

use Zend\Di\DependencyInjector,

$di = new DependencyInjector;
$diDefAggregate = new Definition\Aggregate();

// first add in provided Definitions, for example
$diDefAggregate->addDefinition(new ThirdParty\Dbal\DiDefinition());
$diDefAggregate->addDefinition(new Zend\Controller\DiDefinition());

// for code that does not have TypeHints
$builder = new Definition\BuilderDefinition();
$builder->addClass(($class = Builder\PhpClass));
    ($injectMethod = new Builder\InjectionMethod())
'implementation', 'Class\For\Specific\Implementation'

// now, your application code
$compiler = new Definition\Compiler()
    new Zend\Code\Scanner\DirectoryScanner(__DIR__ . '/App/')
$appDefinition = $compiler->compile();

// now, pass in properties
$im = $di->getInstanceManager();

// this could come from Zend\Config\Config::toArray
$propertiesFromConfig = array( 
    'ThirdParty\Dbal\DbAdapter' => array(
        'username' => 'someUsername', 
        'password' => 'somePassword'
    'Zend\Controller\Helper\ContentType' => array(
        'default' => 'xhtml5'
Previous Next