0

How do I use an object (along with its methods and properties) when I'm inside an object?

Say I have useless classes like these:

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->name = $name;
    }

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

class Eater {
    private $name;

    public function eat($object) {
        $object->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?
    }
}

And I use it as such:

<?php
    $pear = new Fruit("Pear");
    $apple = new Fruit("Apple");

    $paul = new Eater("Paul");
    $paul->eat($apple);
?>

But if I modify the Eater class like so:

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->$varname = $value;
    }

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

And set the main program like so:

<?php
    $pear = new Fruit("Pear");
    $apple = new Fruit("Apple");

    $paul = new Eater("Paul");
    $paul->set('objectToEat',$apple);
    $paul->eat();
?>

How can I access the object's properties from inside a method? I know I use $this->objectToEat 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?

I've tried $this->objectToEat->set('health',0) 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)!

1
  • Sorry, guys! I forgot to include a snippet of code... Commented Jul 22, 2011 at 19:01

6 Answers 6

1

You have to set the property correctly. Since it's private, you can't do this from outside the object, so you have to use encapsulation:

class Eaters {
    private $name;
    private $objectToEat;

    public function eat() {
        $this->objectToEat->set('health',0);    // Assumed "object" was just a typo
    }

    public function setObjectToEat($object) {
        $this->objectToEat = $object;
    }
}

Then use it like so:

<?php
    $pear = new Fruit("Pear");
    $apple = new Fruit("Apple");

    $paul = new Eater("Paul");
    $paul->setObjectToEat($apple);
    $paul->eat();
?>

Note: In this brief example, your original method is a better design. In certain cases, you might want to prime the method to be used by setting properties beforehand, but more often you want to call it with parameters directly, since it's more clear and more reusable (compartmentalized).

Sign up to request clarification or add additional context in comments.

1 Comment

@Greg, yes, I noticed that, it is fixed.
0

This answer modifies Renesis' answer

In the class, the object to eat is a private variable hence you can't go

$paul->objectToEat = $apple;

What you can do is to make a setter method inside Eaters

class Eaters {    
    private $name;    
    private $objectToEat;

    public function eat() {        
        $this->objectToEat->set('health',0); // Assumed "object" was just a typo    
    }

    public function setFood($object) {
        $this->objectToEat = $object;
    }
}

Therefore, you can call the setFood() method instead.

OR

Change eat() to

public function eat($object) {
    $this->object->set('health',0);
    return $object;
}

Saving the modified object back to the original variable.

OR

class Eaters {
    private $name;

    public function eat(&$object) { // this passes object by reference
        $object->set('health', 0);
    }
}

Although this code is not tested, that is how you can pass a variable by reference.

NOTE: You only need the & when defining the method not when you're passing an argument. For more info about Passing by Reference go to this link

1 Comment

That's true about the & operator, but objects don't need to be passed by reference so this doesn't gain you anything in this example. Although there are some differences between object identifiers passed as arguments and passing by reference, they confuse things more than they are useful.
0

It's probably because your eat method isn't accepting any parameters, and the Eaters class has no $object property.

Comments

0

Can you make $objectToEat a reference and then use it as such in the eat() function?

Comments

0

you have to set $this->object in class Eaters

function __construct($object){
    $this->object = $object;
}

or

<?php
    $pear = new Fruit("Pear");
    $apple = new Fruit("Apple");

    $paul = new Eater("Paul");
    $paul->eat($apple);
?>

Comments

0
    class Tester {
       private $variable;
       private $anObj;

       public function testFn($val) {
           $this->variable = $val;
           $this->anObj = new SecondObj();
           $this->doSomething();
       }

       public function doSomething() {
           echo("My variable is set to " . $this->variable);
           $this->anObj->wow();
       }
    }

   class SecondObj {
       public function __construct() {
           echo("I'm new!");
       }
       public function wow() { echo("Wow!"); }
   }

    $tester = new Tester();
    $tester->testFn(42);

Output:

I'm new!My variable is set to 42Wow! 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.