1. Go to this page and download the library: Download mahdrentys/auto-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/ */
mahdrentys / auto-di example snippets
ahdrentys\AutoDI\Container;
class A
{
}
class B
{
public $a;
public function __construct(A $a)
{
$this->a = $a;
}
}
$container = Container::getContainer(); // You can this method throughout all your application, the same container is returned everytime.
$a = $container->get(A::class); // Return an instance of A
$a = $container->get(A::class); // Return the same instance of A, as a singleton
$b = $container->get(B::class); // Return an instance of B (the A dependency is automaticly resolved)
var_dump($b->a); // The same instance of A than $a
class C
{
public $b;
public $name;
public function __construct(string $name, B $b)
{
$this->name = $name;
$this->b = $b;
}
}
$c = $container->get(C::class, 'RandomName'); // You can pass the arguments of the constructor
/**
* The dependencies can be placed before or after the "normal" arguments :
* public function __construct(string $name, B $b)
* OR
* public function __construct(B $b, string $name)
*/
// You can also call functions and methods with auto-wiring
function test(B $b, $someArgument)
{
return $someArgument;
}
$result = $container->call('test', 'someArgument');
class D
{
public function test(B $b, $someArgument)
{
return $someArgument;
}
public static function testStatic($someArgument, B $b)
{
return $someArgument;
}
}
$d = new D();
// Or :
$d = $container->get(D::class);
$result = $container->call([$d, 'test'], 'someArgument');
$result = $container->call('D::testStatic', 'someArgument');
// You can define keys manually
// For example, you have a Database class that
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.