繁体   English   中英

PHP如何从扩展类调用方法

[英]PHP How to call method from extended class

尝试使用扩展单调的对象,但我做不到。 如何从扩展类调用方法?

如何用单例显示 13 个非 12 个?

class SingletonTest
{
    protected static $_instance;

    private function __construct(){}

    private function __clone(){}

    public static function getInstance() {

        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    public function test2(){
        return 12;
    }
}


class ExtendSingleton extends SingletonTest
{
    public function test2() {
        return 13;
    }
}


$b = ExtendSingleton::getInstance();
echo $b->test2(); //12
public static function getInstance()
{
    static $instances = array();

    $calledClass = get_called_class();

    if (!isset($instances[$calledClass]))
    {
        $instances[$calledClass] = new $calledClass();
    }

    return $instances[$calledClass];
}

如果您使用静态绑定关键字“static”而不是“self”,您将得到您想要的

class SingletonTest
{
    protected static $_instance;

    private function __construct(){}

    private function __clone(){}

    public static function getInstance() {

        if (null === static::$_instance) {
            static::$_instance = new static();
        }

        return static::$_instance;
    }

    public function test2(){
        return 12;
    }
}


class ExtendSingleton extends SingletonTest
{
    public function test2() {
        return 13;
    }
}


$b = ExtendSingleton::getInstance();
echo $b->test2(); //13

$a = SingletonTest::getInstance();
echo $a->test2(); //13

exit;

但是正如您在上面的示例中看到的那样,您将首先调用“getInstance”的类将其实例存储在 $_instance 字段中。

无法创建基本单例类并继承单例行为。

所以这应该适合你:

(所以首先通常函数是公共的,所以如果你从另一个类扩展,你可以使用它们!并且你必须从ExtendSingleton而不是从SingletonTest创建一个对象,因为ExtendSingleton exdend's -> SingletonTest而不是其他方式。)

<?php

    class SingletonTest {

        protected static $_instance;

        public function __construct() {

        }

        public function __clone() {

        }

        public static function getInstance() {

            if (null === self::$_instance) {
                self::$_instance = new self();
            }

            return self::$_instance;
        }

        public function test2(){
            return 12;
        }
    }


    class ExtendSingleton extends SingletonTest {

        public function test2() {
            return 13;
        }

    }


    $b = new ExtendSingleton();
    echo $b->test2(); //13


?>

输出:

13

我已经测试了 bicccio 的解决方案并且它有效

class SingletonTest
{
    protected static $_instances = [];

    private function __construct(){}

    private function __clone(){}

    public static function getInstance()
    {
        $calledClass = get_called_class();

        if (!isset(self::$_instances[$calledClass]))
        {
            self::$_instances[$calledClass] = new $calledClass();
        }

        return self::$_instances[$calledClass];
    }

    public function test2(){
        return 12;
    }
}


class ExtendSingleton extends SingletonTest
{
    public function test2() {
        return 13;
    }
}


$b = ExtendSingleton::getInstance();
echo $b->test2(); //13

$a = SingletonTest::getInstance();
echo $a->test2(); //12

您可以使用后期静态绑定在 php 中扩展单例类,整个过程在这个问题中得到了很好的讨论。

在 PHP5 中创建单例设计模式

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM