在PHP5.3后期静态绑定单例方法中调用静态方法

在PHP5.3后期静态绑定单例方法中调用静态方法,php,oop,inheritance,singleton,Php,Oop,Inheritance,Singleton,我有一个包含以下方法的抽象类: <?php public static function getInstance() { $me = get_called_class(); if (!isset(self::$_instances[$me])) { $ref = new ReflectionClass($me); self::$_instances[$me] = $reflection_object->newInstance();

我有一个包含以下方法的抽象类:

<?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;
    }
}
<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>
init()方法是Singleton超类中的一个抽象方法。class Keywords\u AdminMenu\u OptionsTable是一组单独的库(WordPress的WP\u List\u Table)中的类的子类

类别关键字\u AdminMenu\u选项表的分解副本如下所示:

<?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. */

    }
}

原来我的代码没有将getInstance声明为static。这段代码更深入地了解了静态绑定如何影响绑定

其中,如果从静态更改getInstance上的绑定,则会收到以下结果:


我希望这个答案能帮助别人。或者至少提供了关于如何解决此类问题的见解。

这是一个相当大的类,因此我将添加一个链接。我不明白这个问题。在我看来,你的
get\u called\u class
返回
Keywords\u AdminMenu\u options表
,而你正在期待
数据库模块
。是的,这是你想要的设计吗?还是一只虫子?有什么解决方法吗?奇怪的是,在本例中,get_的功能称为_class()。它不返回立即调用的类,也不返回第一个调用的类。它在中间的某个地方。第一个调用的类是Keywords\u AdminMenu,它是对WordPress操作的回调。除非这是一个答案,否则它应该是对您的问题的更新
<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>
*****************************
*        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
*****************************
*        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