Zend - 名称''没有属性存在

时间:2012-05-07 13:50:33

标签: php zend-framework zend-form aptana validation

我要做的是检查从表单发送的日期年份是否具有正确的值。正确的值给出了我作为验证器构造函数中的参数给出的计划年度。

我使用下一个自定义验证器从标题中获取消息。这是因为变量$ this-> $ plan_year,但我不明白为什么。这是验证器:     

class MyValidate_YearValidator extends Zend_Validate_Abstract
{
    const MSG_YEAR = '';

    private $plan_year = 0;

    public $minimum = 0;
    public $maximum = 0;

    protected $_messageVariables = array(
        'min' => 'minimum',
        'max' => 'maximum'
    );

    protected $_messageTemplates = array(
        self::MSG_YEAR => "Valoarea '%value%' nu este corecta! Anul trebuie sa aibe una din urmatoarele doua valori: '%min%' sau '%max%'."
    );

    public function __construct( $plan_year )
    {
        $this->$plan_year = $plan_year;

    }

    public function isValid($value)
    {

        $this->_setValue($value);

        $anul = substr($value, 0, 4);

        //here is the problem
        $this->minimum = $this->$plan_year;
        $this->maximum = $this->$plan_year + 1;

        if ($anul <> $this->minimum && $anul <> $this->maximum && $value != '') {
            $this->_error(self::MSG_YEAR);
            return false;
        }

        return true;
    }
}

谢谢! 索林

1 个答案:

答案 0 :(得分:3)

引用类成员时没有美元符号(只有它们是静态的)!否则,该名称将被视为另一个变量。

$this->$plan_year // wrong
$this->plan_year  // correct