Search code examples
phpcakephpcakephp-3.0getter-setter

Getter method cannot access its own property


I have the following entity Model with property float $price.

When I create such a getter method:

public function _getPrice()
{
    return $this->price*100;
}

It keeps giving me the following error:

Undefined property: App\Model\Entity\Model::$price

What's the problem?


Solution

  • You are essentially trying to access a getter method from within itself.

    In order for this to work, try rewriting it as follows:

    public function _getPrice()
    {
        return $this->_properties['price'] * 100;
    }