0

我在模型 TaiKhoan 中有这个方法

public function getInputFilter()
{
   if (!$this->inputFilter) 
   {
      $inputFilter = new InputFilter();
      $factory     = new InputFactory();

      $inputFilter->add($factory->createInput(array(
            'name'     => 'TenTaiKhoan',
            'required' => true,
            'filters'  => array(
                  array('name' => 'StripTags'),
                  array('name' => 'StringTrim'),
            ),
      )));

      $inputFilter->add($factory->createInput(array(
            'name'     => 'MatKhau',
            'required' => true,
            'filters'  => array(
                  array('name' => 'StripTags'),
                  array('name' => 'StringTrim'),
            ),
      )));
   }

   return $this->$inputFilter;
}

然后我在我的控制器中使用它

$taikhoan = new TaiKhoan();

$form->setInputFilter($taikhoan->getInputFilter());

当我运行时,它告诉我这个错误

Catchable fatal error: Object of class Zend\InputFilter\InputFilter could not be converted to string in C:\wamp\www\ZF\module\CPanel\src\CPanel\Model\TaiKhoan.php on line 59
4

2 回答 2

1

问题是此语句中的错字:

return $this->$inputFilter;

PHP 将此行解释为动态属性名称,并将其转换为字符串。正确的版本是:

return $this->inputFilter;

您还需要为输入过滤器分配一些东西:

public function getInputFilter()
{
    if (!$this->inputFilter) 
    {
        // ...
        $this->inputFilter = $inputFilter;
    }

    return $this->inputFilter;
}
于 2013-02-20T12:03:04.703 回答
0

您延迟加载输入过滤器,但从不设置$this->inputFilter.

public function getInputFilter()
{
    if (!$this->inputFilter)
    {
        $this->inputFilter = new InputFilter();
    }
    return $this->inputFilter;
}

不确定这是否是您的问题,因为错误是关于将输入过滤器转换为字符串。请在 TaiKhoan.php 的第 59 行提供准确的代码。

于 2013-02-20T11:37:09.097 回答