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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:12:48+00:00 2026-06-05T10:12:48+00:00

I am not sure whether the technique I use is appropriate or not. I

  • 0

I am not sure whether the technique I use is appropriate or not.
I have a class, singleton, with a subinstance like this:

final class Singleton {

    public $subinstance;
    private static $instance = NULL;

    private function __construct() {
        $this->subinstance = new subinstance(); 
    }

   public static function getInstance() {

       if (NULL === self::$instance) {
           self::$instance = new self;
       }

   return self::$instance;

   }

   private function __clone() {}
}

And now if I want to access the subinstance from outside the class in another class, I do:

$s = singleton::getInstance();
$s->subinstance->....

Is this the right way to do this?
And what happens when I do $s=singleton::getInstance();, does the entire singleton class gets copied into $s or is this more like a pointer?

  • 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-06-05T10:12:49+00:00Added an answer on June 5, 2026 at 10:12 am

    What you are doing is fine, although in your example code I’m assuming you meant to write:

    $s = Singleton::getInstance(); // with the S capitalized
    

    When you call the getInstance method, the class is looking to see if a version of itself has already been instantiated. If it has, it will return a reference to that instance instead of creating an entirely new instance. If the instance has not been created yet, it will create the instance and then return a reference to it.

    I’m not sure why sixeightzero has stated that you shouldn’t instantiate a class in the constructor of your instance. You shouldn’t experience any issues when doing so.

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

Sidebar

Related Questions

I am not sure whether this only happens to me. Basically if I have
I'm not sure whether or not this is the appropriate way of doing this,
I am not sure whether distinct is the right word for this. I have
Not sure whether this is the right place, but I have a question related
I'm not sure whether this is an issue with my use of cl-who (specifically
I am not sure whether this question is suitable here. Anyway, it seems like
I am not sure whether this is an appropriate question to ask in this
I am not sure whether this is a bug or if I have totally
I'm not sure whether this is an SO question but still would like to
I'm not sure whether I have to make a pivot table or some sub-queries

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.