How do I use objects and access their methods while within an object in PHP?
<p>How do I use an object (along with its methods and properties) when I'm inside an object?</p>

<p>Say I have useless classes like these:</p>

<pre><code>class Fruit {
    private $name;          // Name of the fruit.
    private $health = 10;   // 0 is eaten, 10 is uneaten.
    private $object;        // This is a PHP object.

    public function __construct($name) {
        $this-&gt;name = $name;
    }

    public function set($varname,$value) {
        $this-&gt;$varname = $value;
    }
}

class Eater {
    private $name;

    public function eat($object) {
        $object-&gt;set('health',0);    // I know I can pass and modify objects like this.
        // The object is passed by reference in PHP5 (but not 4), right?
    }
}
</code></pre>

<p>And I use it as such:</p>

<pre><code>&lt;?php
    $pear = new Fruit("Pear");
    $apple = new Fruit("Apple");

    $paul = new Eater("Paul");
    $paul-&gt;eat($apple);
?&gt;
</code></pre>

<p>But if I modify the Eater class like so:</p>

<pre><code>class Eater {
    private $name;
    private $objectToEat;    // Let's say if I need the object to be over here instead of in a method.

    public function set($varname,$value) {
        $this-&gt;$varname = $value;
    }

    public function eat() {
        $this-&gt;objectToEat-&gt;set('health',0);    // This doesn't work!
    }
}
</code></pre>

<p>And set the main program like so:</p>

<pre><code>&lt;?php
    $pear = new Fruit("Pear");
    $apple = new Fruit("Apple");

    $paul = new Eater("Paul");
    $paul-&gt;set('objectToEat',$apple);
    $paul-&gt;eat();
?&gt;
</code></pre>

<p>How can I access the object's properties from inside a method? I know I use <code>$this-&gt;objectToEat</code> to tell PHP I'm talking about the class properity, but since that property is an object, how do I access the object's methods?</p>

<p>I've tried <code>$this-&gt;objectToEat-&gt;set('health',0)</code> but that doesn't work. I hope you guys understand what I'm trying to get at (sorry, I can't figure out how to condense my question without compromising clarity)!</p>
