da2e/simple-factory
Composer 安装命令:
composer require da2e/simple-factory
包简介
Mini-package implementing and providing a Simple Factory (design pattern) functionality.
README 文档
README
SimpleFactory is a mini-package implementing Simple Factory design pattern and providing its functionality.
How to use
<?php use Da2e\SimpleFactory\SimpleFactory; $factory = new SimpleFactory(); $factory->create(TheObjectYouNeedToCreate::class, ['optional', 'array', 'of', 'constructor', 'args']);
The order of the constructor arguments matters!
To automate the process of creating homogeneous objects with the same constructor arguments, you could simply create a wrapper for the SimpleFactory and pass the required arguments to its constructor, as follows:
<?php use Da2e\SimpleFactory\SimpleFactory; class MySimpleFactory extends SimpleFactory { private $dependency; public function __construct($dependency) { $this->dependency = $dependency; } /** * {@inheritdoc} */ public function create(string $class, array $constructorArgs = []) { parent::create($class, array_merge([$this->dependency], $constructorArgs)); } } $factory = new MySimpleFactory('foobar'); $factory->create(TheObjectYouNeedToCreate1::class); $factory->create(TheObjectYouNeedToCreate2::class); $factory->create(TheObjectYouNeedToCreate3::class);
Software requirements
- PHP >= 7.0
How to install
composer require da2e/simple-factory "1.*"
License
This bundle is under the MIT license.
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-05-21