1. Go to this page and download the library: Download nezamy/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/ */
nezamy / di example snippets
class Book
{
private string $name = 'First Book';
public function getName(): string
{
return $this->name;
}
}
$resolver = new Just\DI\Resolver;
$resolver->resolve($resolver->prepare([Book::class, 'getName']));
//Or
$resolver->resolve([new Book, 'getName']);
// the both returns 'First Book'
class Book
{
private string $name = 'First Book';
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
}
$container = Just\DI\Container::instance();
$container->setVar('name', 'PHP');
$book = new Book();
$resolver = new Just\DI\Resolver;
$resolver->resolve([$book, 'setName']);
$book->getName();
// will return 'PHP'
$function = function(Book $book){
return $book->getName();
};
$container = Container::instance();
$resolver = new Resolver;
$name = $resolver->resolve($function);
//here $name returns 'First Book' because it's initial value
$book = new Book();
$book->setName('Test Book');
// for define a singleton object
$container->set(Book::class, $book);
$resolver = new Resolver;
$name = $resolver->resolve($function);
$this->assertSame('Test Book', $name);
//$name now is equal 'Test Book'
class User{
private string $name;
private string $email;
public function __construct(string $name, string $email)
{
$this->name = $name;
$this->email = $email;
}
public function getName(): string
{
return $this->name;
}
public function getEmail(): string
{
return $this->email;
}
}
$container = Just\DI\Container::instance();
$container->setVar('name', 'Mahmoud Elnezamy');
$container->setVar('email', '[email protected]');
$resolver = new Just\DI\Resolver;
$name = $resolver->resolve($resolver->prepare([User::class, 'getName']));
// name here will return 'Mahmoud Elnezamy'
class User{
private string $name;
private string $email;
public function __construct(string $name, string $email)
{
$this->name = $name;
$this->email = $email;
}
public function getName(): string
{
return $this->name;
}
}
class Book
{
public function Auther(User $user){
return $user->getName();
}
}
$container = Just\DI\Container::instance();
$container->setVar('user', ['Mahmoud', '[email protected]']);
$container->setMagicCall(User::class, function ($attr, $value){
return new User(...$value);
});
$resolver = new Just\DI\Resolver;
$book = $resolver->resolve(
$resolver->prepare([Book::class, 'Auther'])
);
//$book will return 'Mahmoud'
$container = \Just\DI\Container::instance();
$container->setVar('name', 'value');
$container->getVar('name');
$container->hasVar('name');
$container->importVars([
'name' => 'name here',
'id' => '1'
]);
$container->set('className', new stdClass());
//Maybe the new instance do some processing or load some configurations or connect with database.
//and you won't to make the instance until the first use or call
$container->set('className', function (){
return new stdClass();
});
$container->get('className');
$container->has('className');
// define some singleton objects
$container->import([
Request::class => new Request(...),
Response::class => new Response(...),
DB::class => new DB('user', 'pass',...)
]);
$container->setMagicCall('UserModel', function ($attr, $value){
if($attr == 'id'){
return new UserModel($value);
}
return null;
});
$container->getMagicCall('UserModel');
$container->hasMagicCall('UserModel');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.