PHP code example of tr33m4n / di

1. Go to this page and download the library: Download tr33m4n/di library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tr33m4n / di example snippets


return [
    Container\GetPreference::CONFIG_KEY => [
        \Some\Interface\To\Implement::class => \Some\Class\To\Bind::class
    ],
    Container\GetParameters::CONFIG_KEY => [
        \Some\Class\To\Bind::class => [
            'testParam' => 'something',
            'anotherClass' => \Some\Class\Argument::class 
        ],
        \Some\Class\Argument::class => [
            'anotherTestParam' => 'somethingElse'
        ]
    ]
];



use tr33m4n\Di\Config;
use tr33m4n\Di\Container;
use tr33m4n\Di\Container\GetParameters;
use tr33m4n\Di\Container\GetPreference;

// Manually initialising the container
$config = new Config();
$container = new Container(new GetParameters($config), new GetPreference($config));

// `get` class from container (class constructor arguments will auto-wire). The instantiated class will be cached for subsequent calls
$myInitialisedClass = $container->get(\Some\Class\To\Get::class);



use tr33m4n\Di\Config;
use tr33m4n\Di\Container;
use tr33m4n\Di\Container\GetParameters;
use tr33m4n\Di\Container\GetPreference;

// Manually initialising the container
$config = new Config();
$container = new Container(new GetParameters($config), new GetPreference($config));

// Create a new instance of a class which is not cached, with all constructor arguments auto-wired
$myInitialisedClass = $container->create(\Some\Class\To\Create::class);

// Create a new instance of a class which is not cached, with all but the `testParam` arguments auto-wired. The `testParam` will be initialised with `something`
$myInitialisedClassWithParameters = $container->create(\Some\Class\To\Create::class, ['testParam' => 'something']);