simple-container
Introduction
This is about the simple container to help developers to understand how the Reflection works.
Usage
Firstly, you have to specify a class that you want to inject.
For example, we assume that you want to inject following Profile
class:
class Profile
{
protected $userName;
public function __construct($userName = 'lee')
{
$this->userName = $userName;
}
public function getUserName()
{
return $this->userName;
}
}
Then we use the Container
class to inject this Profile
class.
use Lee\Container\Container;
$container = new Container();
$container->set(Profile::class);
$profile = $container->get(Profile::class);
echo $profile->getUserName(); // lee
If you want to inject class that its constructor arguments is without the default value, we should specify them by ourselves.
The sample codes are as follows:
class Profile
{
protected $userName;
public function __construct($userName)
{
$this->userName = $userName;
}
public function getUserName()
{
return $this->userName;
}
}
Then we use Container
class to inject this class.
use Lee\Container\Container;
$container = new Container();
$container->set(Profile::class);
$profile = $container->get(Profile::class, ['userName' => 'Peter']);
echo $profile->getUserName(); // Peter
References
This simple-container is about implementing this post.
However, this post we refer is incorrect on some approaches.
We decide to implement this PHP package to complete the correct container example.