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

  • SEARCH
  • Home
  • 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 6028603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:45:51+00:00 2026-05-23T04:45:51+00:00

I have a ParentClass and when I make a new object I want to

  • 0

I have a ParentClass and when I make a new object I want to pass a reference to the ParentClass. (I have to use the ParentClass things in the new object)

I use the constructor to make this object and pass the reference value. (that’s important for me)

But when I use the =& operator, it makes a new instance of the ParentClass, what call the constructor, and then it’s fall an endless recursion.

Here’s my code:

<?php

abstract class MainClass {

    function __construct(&$parent){
        $this->parent =& $parent;
        $this->init();
    }

    abstract protected function init(); 

}

class ParentClass extends MainClass {   

    protected function init(){
        $this->child = new ChildClass($this);
    }

}

class ChildClass extends MainClass {

    protected function init(){}

}


$parent = new ParentClass (new stdClass());
var_dump($parent);

?>

And the result:

object(ParentClass)#1 (2) {
  ["parent"]=>
   object(stdClass)#2 (0) {
  }
  ["child"]=>
   object(ChildClass)#3 (1) {
     ["parent"]=>
     object(ParentClass)#1 (2) {
       ["parent"]=>
       object(stdClass)#2 (0) {
       }
       ["child"]=>
       object(ChildClass)#3 (1) {
         ["parent"]=>
         *RECURSION*
       }
     }
   }
 }

How can I solve this problem?

  • 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-23T04:45:52+00:00Added an answer on May 23, 2026 at 4:45 am

    Objects are passed by reference by default. There is no reason to pass or assign the $parent by reference. So this should be sufficient:

    abstract class MainClass {
    
        function __construct($parent){
            $this->parent = $parent;
            $this->init();
        }
    

    It might be important to you to use &$parent, but it is totally unnecessary.


    Regarding the recursion: There is no recursion in your code, it is recursion in the output.

    This part:

    object(ChildClass)#3 (1) {                // <--- the same element
        ["parent"]=>
        object(ParentClass)#1 (2) {
          ["parent"]=>
          object(stdClass)#2 (0) {
          }
          ["child"]=> 
          object(ChildClass)#3 (1) {          // <--- the same element
            ["parent"]=>
            *RECURSION*
          }
        }
      }
    

    would be printed over and over again, because the child has a reference to its parent and the parent a reference to its child.

    Maybe even more obvious are the repeating numbers in the output:

    object(ParentClass)#1            // 1
      object(stdClass)#2             // 2
      object(ChildClass)#3           // 3
        object(ParentClass)#1        // 1
          object(stdClass)#2         // 2
          object(ChildClass)#3       // 3
            // what would be here? right, object(ParentClass)#1 again
    

    This is normal, there is no problem.

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

Sidebar

Related Questions

I have settings.php file that I want to keep some constants in, like my
I have a function that I want to operate on two different custom objects.
Is it possible to make a static object in a parent class that subclasses
I have the following Model pattern: public abstract class PARENTCLASS {...} public class CHILD_A_CLASS
Let's have the following class hierarchy: public class ParentClass implements SomeInterface { } public
I have a parent class which contains a child object. I am using set
In Django, when you have a parent class and multiple child classes that inherit
I have a private method def __pickSide(self): in a parent class that I would
I have a Ruby class called LibraryItem . I want to associate with every
I have this fluent mapping: sealed class WorkPostClassMap : ClassMap<WorkPost> { public WorkPostClassMap() {

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.