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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:16:27+00:00 2026-05-20T00:16:27+00:00

Why does this not work? Shouldn’t each instance simply reference each other once? class

  • 0

Why does this not work? Shouldn’t each instance simply reference each other once?

class foo {
    private static $instance;
    private function __construct() {
    $test = bar::get_instance();
    }

    public static function get_instance() {
        if (empty(self::$instance)) {
            self::$instance = new foo();
        }
        return self::$instance;
    }
}

class bar {
    private static $instance;
    public function __construct() {
    $test = foo::get_instance();
    }

    public static function get_instance() {
        if (empty(self::$instance)) {
            self::$instance = new bar();
        }
        return self::$instance;
    }
}

$test = foo::get_instance();
  • 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-20T00:16:28+00:00Added an answer on May 20, 2026 at 12:16 am

    You have what’s known as a circular-dependency. A needs B to complete to construct, and B needs A to complete to construct. So it goes round and round forever.

    Basically, what’s happening is that self::$instance on each class doesn’t get populated until new class() finishes. So in the constructor, you’re calling the other getInstance. But every time you hit get_instance(), self::$instance is still null because the previous new never finsihed. And round and round you go. It will keep going until the end.

    Instead, add it in after construction:

    class foo {
        private static $instance;
        private function __construct() {
        }
        private function setBar(bar $bar) {
            $this->bar = $bar;
        }
    
        public static function get_instance() {
            if (empty(self::$instance)) {
                self::$instance = new foo();
                self::$instance->setBar(bar::get_instance());
            }
            return self::$instance;
        }
    }
    
    class bar {
        private static $instance;
        public function __construct() {
        }
        private function setFoo(foo $foo) {
            $this->foo = $foo;
        }
        public static function get_instance() {
            if (empty(self::$instance)) {
                self::$instance = new bar();
                self::$instance->setFoo(foo::get_instance());
            }
            return self::$instance;
        }
    }
    

    However, I would really suggest re-architecting your relationships and classes so that you Inject the Dependencies rather than making self-dependent singletons.

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

Sidebar

Related Questions

I'm trying to understand why this does not work. (Basic example with no validation
I have a textbox with an onchange event. Why does this event not fire
Why does this lambda expression not compile? Action a = () => throw new
My Java application is saving stuff in 'user.home' but on windows this does not
Exception Thrown: System.ComponentModel.ReflectPropertyDescriptor is not marked as Serializable Does this mean I missed marking
I use the Action<object>.BeginInvoke() method, does this use the thread pool or not? I
This code does not seem to compile, I just need to write something to
I hope this question does not come off as broad as it may seem
Note that this function does not have a { and } body. Just a
This is not a technical problem, but very annoying. Does anyone know how to

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.