PHP Method chaining Confusion
<p>i have been lately introduced to method chaining, and am not sure if what I'm doing here is illegal, or I'm doing it wrong. I have a database Class like:</p>

<pre><code>    class Database
    {
    private $connection;
    private $resultset, $last_query, $current_row;

    function __construct()
    {
        $this-&gt;connect();
    }

    public function connect()
    {
        // Connect to the database
    }

    public function query($query)
    {
        $this-&gt;last_query = $query;
        $this-&gt;resultset = mysql_query($query, $this-&gt;connection);
    }

    public function fetchObject()
    {
        $this-&gt;current_row = mysql_fetch_object($this-&gt;resultset);
        return $this-&gt;current_row;
    }
}
</code></pre>

<p>I tried using it like:</p>

<pre><code>$db-&gt;query("SELECT * FROM users WHERE name='JimmyP'")-&gt;fetchObject();
</code></pre>

<p>I also tried </p>

<pre><code>$db-&gt;fetchObject()-&gt;query("SELECT * FROM users WHERE name='JimmyP'");
</code></pre>

<p>But i get the error: "Call to a member function fetchObject() on a non-object"</p>

<p>Can someone please explain to me what Im doing incorrectly here as it relates to using method chaining. Thank you.</p>
