1. Go to this page and download the library: Download comphp/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/ */
comphp / di example snippets
ommonPHP\DependencyInjection\DependencyInjector;
$injector = new DependencyInjector();
// Instantiate a class
$exampleClass = $injector->instantiate(ExampleClass::class);
// Invoke a method
$result = $injector->invoke($exampleClass, 'exampleMethod');
// Call a function or closure
$result = $injector->call('exampleFunction');
ommonPHP\DependencyInjection\DependencyInjector;
$injector = new DependencyInjector();
$object = new stdClass();
$values = ['property1' => 'value1', 'property2' => 'value2'];
// populate public properties only
$injector->populate($object, $values);
// populate all properties
$injector->populate($object, $values, false);
ommonPHP\DependencyInjection\DependencyInjector;
class SpecificClass {
private string $customArg1;
private string $customArg2;
public function __construct(string $customArg1, string $customArg2) {
// Custom construction logic here
}
}
$injector = new DependencyInjector();
// Delegate instantiation of SpecificClass to a custom callback
$injector->delegate(SpecificClass::class, function($injector, $name, $typeName) {
return new SpecificClass('value1', 'value2');
});
// Automatically use the delegate when SpecificClass is needed
$instance = $injector->instantiate(InitiatedClass::class);
PHPUnit 9.6.9 by Sebastian Bergmann and contributors.
...................... 22 / 22 (100%)
Time: 00:00.228, Memory: 4.00 MB
OK (22 tests, 36 assertions)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.