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.

Mark Notton
  • 2,337
  • 1
  • 15
  • 30

1 Answers1

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

Mark Notton
  • 2,337
  • 1
  • 15
  • 30