Twig and magic __get

Created on 16 Jan 2012  路  2Comments  路  Source: twigphp/Twig

I got a BaseEntity class that implements the __call() and __get() functions. In my Twig template, I call entity.name (name doesn't exists in my entity class). The __get() method never gets called by Twig because of this line :

// Template.php line 381
if (isset($object->$item) || array_key_exists($item, $object)) {

isset function returns false, while echo($object->$item) returns the appropriate value.

And because of this line :

// Template.php line 406
} elseif (isset(self::$cache[$class]['methods']['__call'])) {

Twig is trying to call $entity->$property() instead of $entity->$property, which is wrong.

Why is __call magic function supported but not __get ?

Most helpful comment

__get is supported, but you need to implement __isset as well as it is used by the isset() check. Read the documentation here: http://twig.sensiolabs.org/doc/recipes.html#using-dynamic-object-properties

All 2 comments

__get is supported, but you need to implement __isset as well as it is used by the isset() check. Read the documentation here: http://twig.sensiolabs.org/doc/recipes.html#using-dynamic-object-properties

Ok thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Bilge picture Bilge  路  3Comments

koflerdavid picture koflerdavid  路  3Comments

ericmorand picture ericmorand  路  4Comments

CriseX picture CriseX  路  4Comments

Seldaek picture Seldaek  路  6Comments