Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The kind of chaining you are referring to there involves returning not <code>$this</code>, but a new object representing the data found. The trick is to make that object usable <em>as though</em> it were an array or scalar, using <a href="http://php.net/manual/en/language.oop5.magic.php" rel="nofollow">"magic methods"</a> and <a href="http://uk1.php.net/manual/en/reserved.interfaces.php" rel="nofollow">pre-defined interfaces</a>.</p> <p><a href="http://php.net/manual/en/book.simplexml.php" rel="nofollow">The SimpleXML extension</a> makes extensive use of this concept so that every object can simultaneously be used in multiple ways:</p> <ul> <li><code>__toString()</code>, so that casting to a string, or using in an unambiguous string context like <code>echo</code> gives you the text content of an XML node</li> <li><a href="http://uk1.php.net/manual/en/class.arrayaccess.php" rel="nofollow">ArrayAccess</a> so that you can use <code>$a['href']</code> to access attributes, and <code>$li[42]</code> to access one of multiple matching nodes</li> <li><a href="http://uk1.php.net/manual/en/class.iterator.php" rel="nofollow">Iterator</a> so that you can <code>foreach</code> over multiple matches</li> <li><code>__get()</code> and <code>__set()</code> to search for and over-write child elements by tag name</li> </ul> <p>(Actually, SimpleXML is a binary extension, so cheats a bit, but most of its functionality could theoretically be implemented in plain PHP using the above.)</p> <p>So for instance this statement:</p> <pre><code>echo $simplexml_element-&gt;foo[42]-&gt;bar['baz']; </code></pre> <p>appears to contain arrays, hashes, and strings, but is actually a whole chain of object calls, something like this:</p> <pre><code>echo $simplexml_element-&gt;__get('foo')-&gt;offsetGet(42)-&gt;__get('bar')-&gt;offsetGet('baz')-&gt;__toString(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload