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 getters not being called

I am not sure what I'm doing wrong but it seems like I might be missing something obvious. My getters on my model are not working:

class Post extends \Phalcon\Mvc\Model
{
    public $title;

    public function getTitle()
    {
        return "foo";
    }
}

And with the calls below, I expect "foo" to be returned but instead, it returns the original title of the post:

$post = Post::findFirst(1);
echo $post->title; // "original title";

I also tried setting the $title property to protected instead of public but that breaks. Am I missing something obvious?

You must use getters not properties

#post = Post::find(1);
echo $post->getTitle();


9.1k

Ahh, I see. It would make a lot more sense if the framework did that for you. Something along the lines of:

public function __get($prop)
{
    $name = "get" . ucfirst($prop);

    if (method_exists($this, $name)) {
            return $this->$name();
    }
}

This would also mean that any internal property accessors and iterators could also benefit. Cross-referencing this issue on github: https://github.com/phalcon/cphalcon/issues/2027 for a feature request.



8.1k

In order to access protected property, this property must be.