你是如何处理有很多依赖注入的类?在PHP OOP中依赖注入的关联数组依赖关系?
我可以将依赖关系存储在关联数组中,并将它们作为一个进行传递?
class Auth
{
// Set props.
protected $deps;
protected $Database;
protected $Constant;
protected $PasswordHash;
// and so on...
/**
* Construct data.
*/
public function __construct($deps)
{
// Set DI.
$this->deps = $deps;
$this->Database = $this->deps['Database'];
$this->PasswordHash = $this->deps['PasswordHash'];
// and so on...
}
}
在我的容器,
// Set deps.
$deps = [
"Database" => $Database,
"PasswordHash" => new PasswordHash(HASH_COST_LOG2, HASH_PORTABLE),
// and so on...
];
// Instantiate Auth.
// Provide deps.
$Auth = new Auth($deps);
我以前没有做过任何测试与单元测试。那么这个关联阵列依赖关系在DI的设计模式中是可以接受的。它可以被测试吗?
或者有更好的解决方案吗?
你有没有研究过DI容器作为变体,因为你总是会修改你的数组和__construct是否想改变依赖关系? – sergio
是的,我有。看到我上面的编辑。谢谢。 – laukok
不,我的意思是不像简单的数组容器,看起来扔了Symfony \ Dependency Injection,Zend \ Di,PHP-DI等等。他们取代所有的依赖关系。 – sergio