hypario/container
Composer 安装命令:
composer require hypario/container
包简介
A simple container i tried to make like PHP-DI
README 文档
README
What is this ?
This library is a Dependency Injection Container written in PHP. I created this library to learn and using PHP-DI as an exemple
How to use it ?
First you have to create a container builder that will build your container
$builder = new Hypario\Builder(); $container = $builder->build();
then you can use the container to instantiate a class.
for exemple :
class A { public function hello() { return "Hello World !"; } } $builder = new Hypario\Builder(); $container = $builder->build(); $class = $container->get(A::class); echo $class->hello(); // output : "Hello World !"
the container will instantiate the class
But what if i have a constructor ?
Like you would do normally, you sometimes need a constructor for you class, there are different possibilities
With a default value
You sometimes need a class with a constructor which have default values, no problem the class will be instantiated with the default values like so :
class A { private $name; public function __construct(string $name = "John") { $this->name = $name; } public function hello() { return "Hello $this->name !"; } } $builder = new Hypario\Builder(); $container = $builder->build(); $class = $container->get(A::class); echo $class->hello(); // output : "Hello John !"
With a class
Sometimes your class need another class to work, no worry, this container can instantiate the class needed (if the constructor use default values OR a class too !)
class Address { public $address; public function __construct() { $this->address = 'France, Paris 6e' } } class Person { public $name; public $address; public function __construct(Address $address, string $name = 'John') { $this->name = $nom; $this->address = $address; } public function hello() { return "Hello $this->name, you live in $this->address"; } } $builder = new Hypario\Builder(); $container = $builder->build(); $class = $container->get(Person::class); echo $class->hello(); // output : "Hello John, you live in France, Paris 6e"
Definitions
The definitions are an array where you define to the container how to instantiate a class, or what function you have to call for a specific word and so, define how your class should be instantiate.
You maybe thought it was strange to use a container builder instead of directly call the container right ? well in fact, before you build the container, you can define some definitions to the container builder like so :
$builder = new Hypario\Builder(); $builder->addDefinitions(['foo' => 'bar']); $container = $builder->build(); echo $container->get('foo'); // output : "bar"
here I used the definition like a simple array, but you can use those to instantiate a class where you need an Interface
interface testInterface{ public function hello(); } class test implements testInterface { public $string = "Hello I am the test class"; public function hello(): string { return $this->string; } } class A { public $test; public function __construct(testInterface $test){ $this->test = $test; } } $builder = new Hypario\Builder(); $builder->addDefinitions([ testInterface::class => test::class ]); $container = $builder->build(); $class = $container->get(A::class); echo $class->test->hello(); // output : "Hello I am the test class
As we can't get an instance of an interface, as is defined in the definitions, the container will instantiate the test class which implements the testInterface
hypario/container 适用场景与选型建议
hypario/container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 49 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 09 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hypario/container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hypario/container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 49
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-09-08