<p>It's the <code>__toString()</code> magic method:</p>
<blockquote>
<p>The __toString() method allows a class to decide how it will react
when it is treated like a string.</p>
</blockquote>
<p><a href="https://www.php.net/manual/en/language.oop5.magic.php#object.tostring" rel="nofollow noreferrer">https://www.php.net/manual/en/language.oop5.magic.php#object.tostring</a></p>

CC BY-SA 4.0
2020-10-05T10:30:18.040Z
2020-10-05T10:30:18.040Z
Mark Notton
<p>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.</p>
<p><code>{{ craft.entries.id(4).one() }}</code></p>
<p>Returns a simple string like: &quot;About Page&quot;</p>
<p><code>{{ dump(craft.entries.id(4).one()) }}</code></p>
<p>Returns the entire element object.</p>
<p>From a PHP perspective, how does this work? Or where can I find out more about this practice?</p>
<p>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.</p>

<php>
How do entry elements render a default string but also return an object at the same time?
# How do entry elements render a default string but also return an object at the same time?

(Asked by: username_0 on 2020-10-05)

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.

**Tags:** php


---

## 1 answers

### Answer by username_0 on 2020-10-05. Score: 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>


---

