Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6165665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:12:20+00:00 2026-05-23T22:12:20+00:00

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

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:12:21+00:00Added an answer on May 23, 2026 at 10:12 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following ruby code : def use_object(object) puts object.some_method end
I'd like to use the Object Test Bench in VS2008. The docs - and
I have to use the XMLHttp object in classic ASP in order to send
I use MyGeneration along with nHibernate to create the basic POCO objects and XML
Why I can use Object Initializers in Visual Studio 2008 Windows projects, etc targeted
We use a simple object model for our low level networking code at work
I am trying to use a stringstream object in VC++ (VStudio 2003) butI am
I am trying to use a business object that I am passing to the
When I use an Iterator of Object I use a while loop (as written
I'm wondering how to use a VideoDisplay object (defined in MXML) to display video

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.