How can I achieve something like $app->error()->encode() in PHP?
<p>I want to include this function in a class. This function will essentially <code>json_encode</code> the output of the previous function (I hope that makes sense).</p>

<p>I want to do something like this:</p>

<pre><code>&lt;?php

$app = new App;

// $app-&gt;error(); // Should return ['some error', 'some other error']

echo $app-&gt;error()-&gt;encode(); // Should return {'errors': ['some error', 'some other error']}.
</code></pre>

<p>Also what's the correct term for such function? I've been searching but couldn't find anything as I didn't really know what I was looking for. </p>

<p>Thanks!</p>

<p><strong>Edit</strong></p>

<p>I think you got that wrong. It's my mistake I didn't mention it before.</p>

<p>That's just an example. Just like in frameworks like Slim, where you can do something like:</p>

<pre><code>$response-&gt;getBody()-&gt;write('Something');
</code></pre>

<p>I want to do <em>something similar</em> to that. Not just that. I want to learn how it's done. </p>