We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Model protected properties - Bug?

As this does not throw an error;

class Robots extends \Phalcon\Mvc\Model {
    protected $name;

    public function setName($name)
    {
        $this->name = $name;
    }
}

$robot = new Robots();
$robot->name = "Bumblebee";
$robot->save();

but the following does;

// Notice: Access to undefined property ...
echo $robot->name;

should I override the magic __set() function (in Robots) and throw an Exception, or is it a bug?



98.9k
Accepted
answer

You should override the set method, this method is currently used by the ORM to set records on related relations. Since the set method is defined on the same model it can write the property due to its protected visibility.

Thanks - from Sunny Australia :)