Zend_Form验证器语法错误 - 这是否违反了php类规则?

时间:2012-01-17 17:59:34

标签: php zend-framework class

我正在Zend框架中创建一个自定义验证器。

const MSG_MINIMUM = 'msgMinimum';
const MSG_MAXIMUM = 'msgMaximum';
const MSG_NUMERIC = 'msgNumeric';

protected $_config = null;
protected $_min = 0;
protected $_max = 0;

protected $_messageTemplates = array(
  self::MSG_MINIMUM => "You must have at least ".$this->_min." selected",
  self::MSG_MAXIMUM => "Too many, ".$this->_max." selected",
  self::MSG_NUMERIC => "'%value%' is not a valid number"
);

public function __construct(Zend_Config $config)
{
  $this->_config = $config;
  $this->_min = $this->_config->limit->orderMin;
  $this->_max = $this->_config->limit->orderMax;
}

知道为什么这行是语法错误?

self::MSG_MINIMUM => "You must have at least ".$this->_min." selected",

我有一种感觉,我违反了阶级规则。

1 个答案:

答案 0 :(得分:2)

类体中的属性声明不能包含表达式。它们必须只是静态值。您必须在构造函数中初始化$_messageTemplates

像这样:

// ...

protected $_messageTemplates;

public function __construct(Zend_Config $config)
{

  // ...

  $this->_messageTemplates = array (
    self::MSG_MINIMUM => "You must have at least ".$this->_min." selected",
    self::MSG_MAXIMUM => "Too many, ".$this->_max." selected",
    self::MSG_NUMERIC => "'%value%' is not a valid number"
  );

}