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
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.
Returns the result:
Where if you change the binding on getInstance from static you receive a result of:
I hope this answer helps somebody out. Or at least gives insight as to how to go about troubleshooting such a problem.