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 658791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:58:21+00:00 2026-05-13T22:58:21+00:00

is there a way to traverse an object to get the parent object data?

  • 0

is there a way to traverse an object to get the parent object data?
With “parent object” I don’t mean the parent class, but literally object.
Here an example, in a javascripty world 🙂 :

$parent->test = "hello world!";

$parent->child = new B();

It would be great If I could access all the data from parent in the child object:

class B{

//I know this doesn't exists, but it's what I wanted do do
    function B(){
        $this->parent = $this->parent();
        echo $this->parent->test; //it would ouput "hello world"
    }
}

For now my solution is to pass the parent object to the child (as a reference) or to make the parent global.
Do you have any better solution?

Thanks!

  • 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-13T22:58:21+00:00Added an answer on May 13, 2026 at 10:58 pm

    There is no way to invoke

    $parent->test = "hello world!";
    $parent->child = new B();
    

    and automatically have a reference to $parent in B.


    Generally, there is four ways to structure your classes:

    1. Aggregate the parent object via Injection, e.g.

    class B
    {
         private $parent;
    
         public function __construct($parent) 
         {
             $this->parent = $parent;
         }
    
         public function setParent($parent) 
         {
             $this->parent = $parent;
         }
    
         public function accessParent() 
         {
            $this->parent->someMethodInParent();
         }
    }
    

    Use constructor injection when the object has to have a parent when it’s created. This is a has-a relationship and it creates a very loose coupling. There is no hardcoded dependencies in B, so you can easily swap out the Parent instance, for instance with a Mock when UnitTesting. Using Dependency Injection will make your code more maintainable.

    In your UseCase, you’d pass $parent to B when creating B:

    $parent->child = new B($parent);
    

    2. Use Composition

    class B
    {
         private $parent;
    
         public function __construct() 
         {
             $this->parent = new Parent;
         }
    
         public function accessParent() 
         {
            $this->parent->someMethodInParent();
         }
    }
    

    This is also a has-a relationship, but couples the Parent class to B. It’s also not an existing Parent instance, but a new instance. From the wording I find it somewhat odd to have a parent created by the child. Use this, when dependency is a class that is not considered to exist outside of the root class, but is part of the whole thing it represents.

    For your UseCase, there is no way of doing $parent->child = new B(); and know what parent is when using this approach, unless $parent is a Singleton. If so, you could get the Singleton instance, e.g. Parent::getInstance() to achieve what you want, but note that Singletons are not everyone’s favorite pattern, e.g. hard to test.

    3. Use Inheritance

    class B extends Parent 
    {
        public function accessParent() 
        {
            $this->someMethodInParent();
        }
    }
    

    This way you create an is-a relationship. All public and protected methods and properties from the Parent class, (but not of a specific instance) will be available in B and you can access them via the $this keyword of the B instance.

    For your UseCase, this approach is not working, as you don’t have to have an instance of Parent at all, but B would encapsulate everything of Parent when it’s created

    $b = new B;
    

    4. Use global keyword

    class B extends Parent 
    {
        private $parent;
    
        public function __construct() 
        {
            global $parent;
            $this->parent = $parent;
        }
    
        public function accessParent() 
        {
            $this->parent->someMethodInParent();
        }
    }
    

    The global keyword imports global variables into the current scope. In general, you should avoid using the global keyword in an OO context, but use one of the other three methods above, preferably the first one. While it’s a language feature, it’s frowned upon – although it is the next closest thing to the first one, e.g.

    $parent->child = new B();
    

    Anyway, hope that helps.

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

Sidebar

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.