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

Calling methods on array element

Hi all,

i don't know if it is a bug but i'm not able to call any method over an object stored in array: The following code compiles in a wrong PHP code wich won't work: {{ my_array['my_object'].my_method() }}

The result of the compilation is like: <?php echo ($my_array['my_object'])->my_method(); ?>

As you can see the extra parentheses surrounding $my_array['my_object'] will result in a PHP Fatal Error.

As a workaround i've the need every time to use a temp var like this {% set my_obj = my_array['my_object'] %} {{ my_obj.my_method() }}

I suppose VOLT should not add those parentheses so far. Thanks

It can be a bug, file a ticket at github. Normally you just pass all objects into your view or Do if they are services, and then call methods on'em. No need for arrays.



20.4k

Thanks for the replay but i suppose that arrays are often used to display several objects of the same type. Even single objects are needed to be passed to view and no problem with that.

Regards

If they are so if the same type, then just iterate through them. Each time you enter a new iteration, you'll get an instance you can call methods directly on.

See for-loops in Volt documentation.