PHP code example of fastero / php-di

1. Go to this page and download the library: Download fastero/php-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/ */

    

fastero / php-di example snippets



$di = new \Fastero\DependencyInjector\DependencyInjector();

$user = $di->get(User::class);


class Company{

}
class User{
    public $company;
    public $language;
    //typehint Company says that first parameter expected to be of 
    //type Company and DependencyInjetor can understand this
    public function __construct(Company $userCompany, $language = null)
    {
        $this->company = $userCompany;
        $this->language = $language;
    }
}

class UserFactory{
    public static function create(){
        return new User(new Company(), 'en');
    }
}


$configuration = [
        "services" =>[
            Company::class => ServiceConfiguration::setupClass(Company::class) //service that will create object of Company
                ->get(),//return complete configuration, should be very last call for every definition
            User::class => ServiceConfiguration::setupClass(User::class) //service that will create object of User
                ->addParameterService(Company::class) //first parameter in the constructor will service with name Company::class
                ->addParameterValue('fr') //second parameter will be value "fr"
                ->get(),//return complete configuration
            'forty-two' => ServiceConfiguration::setupClosure(function ($di, $serviceName){//closure will be called and returned value is a service
                        return 42;
                    })
                ->get(),
        ]
    ];

$configuration = [
        "services" =>[
            Company::class => ServiceConfiguration::setupClass(Company::class) //service that will create object of Company
                ->get(),//return complete configuration, should be very last call for every definition
            User::class => ServiceConfiguration::setupClass(User::class) //service that will create object of User
                ->addParameterService(Company::class) //first parameter in the constructor will service with name Company::class
                ->addParameterValue('fr') //second parameter will be value "fr"
                ->get(),//return complete configuration
            'forty-two' => ServiceConfiguration::setupClosure(function ($di, $serviceName){//closure will be called and returned value is a service
                        return 42;
                    })
                ->get(),
        ]
    ];
    

$di = new Fastero\DependencyInjector\DependencyInjector($configuration);

//if you need to define some service after configuration is set there is a way
//here we create service type factory but sure thing it can be any supported type
$UserEngServiceConfiguration =
    ServiceConfiguration::setupFactory([UserFactory::class, 'create'])// call_user_function_array([UserFactory::class, 'create'], $params) will be called and return value is a service
        ->get();

$di->setServiceConfiguration('UserEng', $UserEngServiceConfiguration);
$service = $di->get(User::class );

var_dump($service instanceof User);
var_dump($service->language);

$service = $di->get("UserEng" );
var_dump($service instanceof User);
var_dump($service->language);

$service = $di->get("forty-two");
var_dump($service);