我正在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",
我有一种感觉,我违反了阶级规则。
答案 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"
);
}