1
votes

I am trying to print an object's property on my template:

{{ MyObject.itsProperty }}

The thing is, this property is not set, __isset will return false and __get will return null.

Instead of printing nothing and leave it at that, Twig tries to print MyObject which causes an error:

Recoverable fatal error: Object of class MyObject could not be converted to string in .....vendor/twig/twig/lib/Twig/Environment.php(378) : eval()'d code on line 54

Setting strict_variables to false didn't help. How to deal with it?

1
I've never seen that behavior...as a hacky workaround you could define __toString() for the object and have it return an empty string... - TheGentleman
Do you have a getItsProperty(), hasItsProperty() or isItsProperty() function implemented in MyObject class? - iiirxs
Have you tried {{ MyObject.itsProperty|default('') }}? - Paul
@Adinan, hence "hacky workaround". Does MyObject have a __call() method? Twig will try that too when trying to resolve a dot operator. - TheGentleman
@GentlemanMax ahaha.... as matter of fact it did... and returning $this .... that explains it. Thank you - Adinan

1 Answers

0
votes

As @GentlemanMax pointed out, Twig will try calling methods as well as properties in attempt to resolve a dot operator. Which may result in callings to the __get and __call magic methods.

My problem was my implementation of __call which was returning $this, resulting in the error I mention above.

Thank you @GentlemanMax and everybody else for helping me out.