从属性getter的返回值实例化类

时间:2011-12-14 07:09:22

标签: php oop zend-framework

我今天遇到了一些奇怪的行为,围绕从getter方法的返回值创建一个对象。

首先我尝试了这个

$productMapper = new $this->getDbMapper();

这给了我一个未定义的属性:$ getDbMapper error。

从它的外观来看,它试图将$ getDbMapper作为类属性访问,然后使用()进行类实例化而不是方法(?)。

我在代码的另一部分也有这个问题,其中类采用构造函数参数。

这是一个很好的时间来看看顶部的某种工厂模式还是我错过了什么?

1 个答案:

答案 0 :(得分:0)

我发现了一个修复,我对此并不是特别满意

$mapper = $this->getDbMapper();
$productMapper = new $mapper();

// With a class constructor
$form = $this->getForm();
$form = new $form($productData);
相关问题