1
服务我有了一个构造函数,采用参数的PHP类:ZF2 - 我如何通过构造函数参数在Module.php
例如:
Users.php
namespace Forms;
class Users
{
protected $userName;
protected $userProperties = array();
public function __construct($userName, array $userProperties = null)
{
$this->userName = $userName;
$this->userProperties = $userProperties;
}
public function sayHello()
{
return 'Hello '.$this->userName;
}
}
现在,我想在这样一个模型文件使用这个类:
$form = new Forms\Users('frmUserForm', array(
'method' => 'post',
'action' => '/dosomething',
'tableWidth' => '800px'
));
它工作得很好。但是,为了编写单元测试,我需要将其重构为Service Factory,以便我可以嘲笑它。
所以,我的服务工厂现在看起来是这样的:
public function getServiceConfig()
{
return array(
'initializers' => array(
function ($instance, $sm)
{
if ($instance instanceof ConfigAwareInterface)
{
$config = $sm->get('Config');
$instance->setConfig($config[ 'appsettings' ]);
}
}
),
'factories' => array(
'Forms\Users' => function ($sm)
{
$users = new \Forms\Users();
return $users;
},
)
);
}
有了这个重构的地方,我有两个问题:
- 如何使用窗体\用户服务示范考虑ServiceLocator的文件在模型文件中不可用?
- 如何在实例化模型中的Users类时更改Service Factory实例以为构造函数提供参数。