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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:37:24+00:00 2026-05-25T20:37:24+00:00

I have an abstract class that contains these methods: <?php public static function getInstance()

  • 0

I have an abstract class that contains these methods:

<?php
public static function getInstance() {
    $me = get_called_class();
    if (!isset(self::$_instances[$me])) {
        $ref = new ReflectionClass($me);
        self::$_instances[$me] = $reflection_object->newInstance();
        self::$_instances[$me]->init();
    }
    return self::$_instances[$me];
}

public function __construct() {
    $me = get_class($this);
    if(isset(self::$_instances[$me])) {
        throw new Exception('The singleton class has already been instantiated!');
    } else {
        self::$_instances[$me] = $this;
        $this->_className = $me;
    }
}

It works exactly as I had expected when instantiating within sibling singletons. I am having an issue when attempting to get an instance from a child class that does not share the same superclass.

My stack trace is:

Fatal error: Call to undefined method Keywords_AdminMenu_OptionsTable::init() in D:\webroot\domains\dev.slbmeh.com\htdocs\wordpress\wp-content\plugins\LocalGiant_WPF\library\LocalGiant\Module\Abstract.php on line 149

Call Stack:
    0.0012     331664   1. {main}() D:\webroot\domains\dev.slbmeh.com\htdocs\wordpress\wp-admin\admin.php:0
    0.7772    3224864   2. do_action() D:\webroot\domains\dev.slbmeh.com\htdocs\wordpress\wp-admin\admin.php:151
    0.7774    3225792   3. call_user_func_array() D:\webroot\domains\dev.slbmeh.com\htdocs\wordpress\wp-includes\plugin.php:405
    0.7774    3225808   4. Keywords_AdminMenu->showMenu() D:\webroot\domains\dev.slbmeh.com\htdocs\wordpress\wp-includes\plugin.php:405
    0.7796    3227016   5. Keywords_AdminMenu_View::showMenu() D:\webroot\domains\dev.slbmeh.com\htdocs\wordpress\wp-content\plugins\LocalGiant_WPF\modules\Keywords\AdminMenu.php:29
    0.7821    3240776   6. Keywords_AdminMenu_OptionsTable->prepareItems() D:\webroot\domains\dev.slbmeh.com\htdocs\wordpress\wp-content\plugins\LocalGiant_WPF\modules\Keywords\AdminMenu\View.php:25
    0.7822    3241824   7. LocalGiant_Module_Abstract->getInstance() D:\webroot\domains\dev.slbmeh.com\htdocs\wordpress\wp-content\plugins\LocalGiant_WPF\modules\Keywords\AdminMenu\OptionsTable.php:90

The init() method is an abstract method in the Singleton superclass. The class Keywords_AdminMenu_OptionsTable is a subclass of a class from a separate set of libraries, WP_List_Table from WordPress.

A broken down copy of the class Keywords_AdminMenu_Options table is as follows:

<?php
class Keywords_AdminMenu_OptionsTable extends WP_List_Table {

    public function __construct(){
        global $status, $page;

        //Set parent defaults
        parent::__construct( array(
            'singular'  => 'module',
            'plural'    => 'modules',
            'ajax'      => false
        ) );

    }

    function prepareItems() {

        /* SNIP - Prepare my SQL query. */

        $moduleDatabase = Database_Module::getInstance();

        $current_page = $this->get_pagenum();

        $keywords = $moduleDatabase->simpleQuery($sql, $moduleLoader->getMyNamespace());

        /* SNIP - Handle my SQL data. */

    }
}

The contents of WP_List_Table found here: http://core.trac.wordpress.org/browser/tags/3.2.1//wp-admin/includes/class-wp-list-table.php

  • 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-25T20:37:25+00:00Added an answer on May 25, 2026 at 8:37 pm

    It turns out my code did not have getInstance declared static. This snippet of code gives a little more insight to how the static bindings affect the bindings.

    <pre><?php
    
    class Singleton_Super {
    
        protected static $_instances = array();
        protected $_className;
    
        public function singletonSuperTest() {
    
            $getCalled = get_called_class();
            $getClass  = get_class($this);
            $getName   = $this->_className;
    
            echo "\nSingleton_Super::singletonSuperTest()\n";
            echo "\tget_called_class() = $getCalled\n";
            echo "\tget_class(\$this) = $getClass\n";
            echo "\t_className() = $getName\n";
    
        }
    
        public function whatsMyName() {
            echo "\nSingleton_Super::whatsMyName()\n\t$this->_className\n";
        }
    
        public static function getInstance() {
            $me = get_called_class();
            echo "\nSingleton_Super::getInstance()\n\tget_called_class() = $me\n";
            if (!isset(self::$_instances[$me])) {
                $ref = new ReflectionClass($me);
                self::$_instances[$me] = $ref->newInstance();
            }
            return self::$_instances[$me];
        }
    
        public function __construct() {
            $me = get_class($this);
            echo "\nSingleton_Super::__construct()\n\tget_called_class() = $me\n";
            if(isset(self::$_instances[$me])) {
                throw new Exception("The Singleton class, '$me', has already been instantiated.");
            } else {
                self::$_instances[$me] = $this;
                $this->_className = $me;
            }
        }
    
        public function __clone() {
            trigger_error("Cloning is not allowed.", E_USER_ERROR);
        }
    
        public function __wakeup() {
            trigger_error("Unserializing is not allowed.", E_USER_ERROR);
        }
    
    }
    
    class Singleton_Child extends Singleton_Super {
    
        public function singletonTest() {
    
            $getCalled = get_called_class();
            $getClass  = get_class($this);
            $getName   = $this->_className;
    
            echo "\nSingleton_Child::singletonTest()\n";
            echo "\tget_called_class() = $getCalled\n";
            echo "\tget_class(\$this) = $getClass\n";
            echo "\t_className() = $getName\n";
    
        }
    
    }
    
    class Outer_Concrete_Super {
    
        protected $_className;
    
        public function __construct() {
            $me = get_class($this);
            $this->_className = $me;
            echo "\nOuter_Concrete_Super::__construct()\n\tget_class(\$this) = $me\n";
        }
    
        public function whatsMyName() {
            echo $this->_className;
        }
    
        public function concreteSuperTest() {
    
            $getCalled = get_called_class();
            $getClass  = get_class($this);
            $getName   = $this->_className;
    
            echo "\nOuter_Concrete_Super::concreteSuperTest()\n";
            echo "\tget_called_class() = $getCalled\n";
            echo "\tget_class(\$this) = $getClass\n";
            echo "\t_className() = $getName\n";
    
        }
    
        public function superUseSingletonChild() {
            $singleton = Singleton_Child::getInstance();
            $singleton->singletonTest();
        }
    
    }
    
    class Outer_Concrete_Child extends Outer_Concrete_Super {
    
        public function concreteTest() {
    
            $getCalled = get_called_class();
            $getClass  = get_class($this);
            $getName   = $this->_className;
    
            echo "\nOuter_Concrete_Super::concreteSuperTest()\n";
            echo "\tget_called_class() = $getCalled\n";
            echo "\tget_class(\$this) = $getClass\n";
            echo "\t\$_className = $getName\n";
    
        }
    
        public function bigTest() {
            $singleton = Singleton_Child::getInstance();
            $singleton->whatsMyName();
            $singleton->singletonSuperTest();
            $singleton->singletonTest();
        }
    
    }
    
    echo "\n*****************************\n";
    echo   "*        Constructors       *\n";
    echo   "*****************************\n\n";
    
    $singleP = Singleton_Super::getInstance();
    $singleC = Singleton_Child::getInstance();
    $outerP = new Outer_Concrete_Super;
    $outerC = new Outer_Concrete_Child;
    
    echo "\n*****************************\n";
    echo   "* Third Party Class Testing *\n";
    echo   "*****************************\n\n";
    
    $outerP->concreteSuperTest();
    $outerC->concreteSuperTest();
    $outerC->concreteTest();
    $outerC->bigTest();
    
    echo "\n*****************************\n";
    echo   "*         Singleton         *\n";
    echo   "*****************************\n\n";
    
    $singleP->whatsMyName();
    $singleP->singletonSuperTest();
    $singleC->whatsMyName();
    $singleC->singletonSuperTest();
    $singleC->singletonTest();
    
    ?>
    </pre>
    

    Returns the result:

    *****************************
    *        Constructors       *
    *****************************
    
    
    Singleton_Super::getInstance()
            get_called_class() = Singleton_Super
    
    Singleton_Super::__construct()
            get_called_class() = Singleton_Super
    
    Singleton_Super::getInstance()
            get_called_class() = Singleton_Child
    
    Singleton_Super::__construct()
            get_called_class() = Singleton_Child
    
    Outer_Concrete_Super::__construct()
            get_class($this) = Outer_Concrete_Super
    
    Outer_Concrete_Super::__construct()
            get_class($this) = Outer_Concrete_Child
    
    *****************************
    * Third Party Class Testing *
    *****************************
    
    
    Outer_Concrete_Super::concreteSuperTest()
            get_called_class() = Outer_Concrete_Super
            get_class($this) = Outer_Concrete_Super
            _className() = Outer_Concrete_Super
    
    Outer_Concrete_Super::concreteSuperTest()
            get_called_class() = Outer_Concrete_Child
            get_class($this) = Outer_Concrete_Child
            _className() = Outer_Concrete_Child
    
    Outer_Concrete_Super::concreteSuperTest()
            get_called_class() = Outer_Concrete_Child
            get_class($this) = Outer_Concrete_Child
            $_className = Outer_Concrete_Child
    
    Singleton_Super::getInstance()
            get_called_class() = Singleton_Child
    
    Singleton_Super::whatsMyName()
            Singleton_Child
    
    Singleton_Super::singletonSuperTest()
            get_called_class() = Singleton_Child
            get_class($this) = Singleton_Child
            _className() = Singleton_Child
    
    Singleton_Child::singletonTest()
            get_called_class() = Singleton_Child
            get_class($this) = Singleton_Child
            _className() = Singleton_Child
    
    *****************************
    *         Singleton         *
    *****************************
    
    
    Singleton_Super::whatsMyName()
            Singleton_Super
    
    Singleton_Super::singletonSuperTest()
            get_called_class() = Singleton_Super
            get_class($this) = Singleton_Super
            _className() = Singleton_Super
    
    Singleton_Super::whatsMyName()
            Singleton_Child
    
    Singleton_Super::singletonSuperTest()
            get_called_class() = Singleton_Child
            get_class($this) = Singleton_Child
            _className() = Singleton_Child
    
    Singleton_Child::singletonTest()
            get_called_class() = Singleton_Child
            get_class($this) = Singleton_Child
            _className() = Singleton_Child
    

    Where if you change the binding on getInstance from static you receive a result of:

    *****************************
    *        Constructors       *
    *****************************
    
    
    Singleton_Super::getInstance()
            get_called_class() = Singleton_Super
    
    Singleton_Super::__construct()
            get_called_class() = Singleton_Super
    
    Singleton_Super::getInstance()
            get_called_class() = Singleton_Child
    
    Singleton_Super::__construct()
            get_called_class() = Singleton_Child
    
    Outer_Concrete_Super::__construct()
            get_class($this) = Outer_Concrete_Super
    
    Outer_Concrete_Super::__construct()
            get_class($this) = Outer_Concrete_Child
    
    *****************************
    * Third Party Class Testing *
    *****************************
    
    
    Outer_Concrete_Super::concreteSuperTest()
            get_called_class() = Outer_Concrete_Super
            get_class($this) = Outer_Concrete_Super
            _className() = Outer_Concrete_Super
    
    Outer_Concrete_Super::concreteSuperTest()
            get_called_class() = Outer_Concrete_Child
            get_class($this) = Outer_Concrete_Child
            _className() = Outer_Concrete_Child
    
    Outer_Concrete_Super::concreteSuperTest()
            get_called_class() = Outer_Concrete_Child
            get_class($this) = Outer_Concrete_Child
            $_className = Outer_Concrete_Child
    
    Singleton_Super::getInstance()
            get_called_class() = Outer_Concrete_Child
    
    Outer_Concrete_Super::__construct()
            get_class($this) = Outer_Concrete_Child
    Outer_Concrete_Child
    
    Fatal error:  Call to undefined method Outer_Concrete_Child::singletonSuperTest() in C:\inetpub\vhosts\BetterOffLocal.com\httpdocs\wp-content\plugins\LocalGiant_WPF\Singleton-Test.php on line 128
    

    I hope this answer helps somebody out. Or at least gives insight as to how to go about troubleshooting such a problem.

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

Sidebar

Related Questions

I have an abstract class that implements IDisposable, like so: public abstract class ConnectionAccessor
I have a class that inherits from Page, called APage. public abstract class APage:
If I have a base class such that public abstract class XMLSubscription <T extends
I have a abstract base C# class with a couple of methods that has
I have an abstract class that has a virtual method. The method is virtual
I have an abstract base class that holds a Dictionary. I'd like inherited classes
I have a abstract base class that I have many inherited classes coming off
Ok so I have an abstract base class called Product, a KitItem class that
What if I have a class that both extends an abstract class and implements
I have an abstract class, Foo, that has a non-abstract method called Bar. I

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.