lee/simple-container
Composer 安装命令:
composer require lee/simple-container
包简介
PHP library that reflect the specific classes
README 文档
README
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.
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 14
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-02-26