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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:14:07+00:00 2026-05-25T15:14:07+00:00

class A{ private static $instance; public static function getInstance(){ if(!(self::$instance instanceof self)) self::$instance =

  • 0
class A{
  private static $instance;

  public static function getInstance(){

    if(!(self::$instance instanceof self))
      self::$instance = new self();

    return self::$instance;
  }

  public function doStuff(){
    echo 'stuff';
  }


}

class B extends A{
  public function doStuff(){
    echo 'other stuff';
  }
}

A::getInstance()->doStuff(); // prints "stuff"

B::getInstance()->doStuff(); // prints "stuff" instead of 'other stuff';

What am I doing wrong?

Why doesn’t class B run it’s function?

  • 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-25T15:14:07+00:00Added an answer on May 25, 2026 at 3:14 pm

    Look at the code in getInstance:

     if(!(self::$instance instanceof self))
           self::$instance = new self();
    

    All those selfs point to A, not to the class that was called. PHP 5.3 introduces something called “late static binding”, which allows you to point to the class that was called, not to the class where the code exists. You need to use the static keyword:

    class A{
      protected static $instance;  // converted to protected so B can inherit
    
      public static function getInstance(){
        if(!(static::$instance instanceof static))
          static::$instance = new static(); // use B::$instance to store an instance of B
    
        return static::$instance;
      }
    
      public function doStuff(){
        echo 'stuff';
      }
    }
    

    Unfortunately, this will fail if you don’t have PHP 5.3 at least.

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

Sidebar

Related Questions

class ConfigReader { private static $instance = NULL; protected $configData = array(); public function
I have this class: public static class CsvWriter { private static StreamWriter _writer =
This isn't valid code: public class MyClass { private static boolean yesNo = false;
Here's a simplified version of my class: public abstract class Task { private static
Please consider the following java source: package com.stackoverflow; public class CondSpeed { private static
class TsDatabasePool { private: TsDatabasePool(int numDBConn, std::string& DBName, std::string& DBType); static TsDatabasePool* objInst_; public:
I have this code running on PHP 5.2.6 class Singleton { static private $instance
I write a singleton c++ in the follow way: class A { private: static
Consider this sample class, class TargetClass { private static String SENSITIVE_DATA = sw0rdfish; private
The default way to implement singleton pattern is: class MyClass { private static MyClass

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.