1

So this is more of a PHP question than a Craft question, but the only example I can offer is one I've seen in Craft.

{{ craft.entries.id(4).one() }}

Returns a simple string like: "About Page"

{{ dump(craft.entries.id(4).one()) }}

Returns the entire element object.

From a PHP perspective, how does this work? Or where can I find out more about this practice?

It's a tricky thing to look up online and tracing through Craft's codebase hasn't offered much insight for a novice like me.

2

It's the __toString() magic method:

The __toString() method allows a class to decide how it will react when it is treated like a string.

https://www.php.net/manual/en/language.oop5.magic.php#object.tostring

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.