【问题标题】:Best practice to implement static class inheritance? (singleton)实现静态类继承的最佳实践? (单件)
【发布时间】:2011-07-14 09:23:26
【问题描述】:

来自php手册:

[...] 静态方法调用在编译时解析。 当使用显式类名时,该方法已经被完全识别并且没有 继承规则适用。如果调用是由 self 完成的,则 self 被转换为 当前类,即代码所属的类。 这里也没有继承规则适用[...]

..所以我正在寻找一种方法来模拟 标准 oop 继承与静态单例。

代码解释得更好:

// Normal inheritance: my goal.
class Foo{
    public function test(){
        echo "Foo->test()\n";
    }
}

class Bar extends Foo{
    public function other_test()
    {
        echo "Bar->other_test()\n";
    }
}


$obj = new Bar();
echo get_class($obj) . "\n";
$obj->test();
$obj->other_test();
/*
 Output:
Bar
Foo->test()
Bar->other_test()
*/


// How i would love to do:
class Foo2{
    public static function test2()
    {
        echo "Foo2::test2()\n";
    }

    // Singleton?
    public static $_instance;
    public static function get_instance()
    {
        if(is_null(self::$_instance))
        {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
}

class Bar2 extends Foo2{
    public static function other_test2()
    {
        echo "Bar2::other_test2()\n";
    }
}

$obj2 = Bar2::get_instance();
echo get_class($obj2) . "\n";
$obj2::test2();
$obj2::other_test2();
/*
 Output:
Foo2
Foo2::test2()
Fatal error: Call to undefined method Foo2::other_test2()
*/

echo "\n-------\n";

// How im doing actually:
interface Foo3{
    public static function get_instance();
}

class Bar3 implements Foo3{
    // Singleton?
    public static $_instance;
    public static function get_instance()
    {
        if(is_null(self::$_instance))
        {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    public static function test3()
    {
        echo "Bar3::test3()\n";
    }
    public static function other_test3()
    {
        echo "Bar3::other_test3()\n";
    }
}


$obj3 = Bar3::get_instance();
echo get_class($obj3) . "\n";
$obj3::test3();
$obj3::other_test3();
/*
 Output:
Bar3
Foo3::test3()
Bar3::other_test3()
*/

最后的“方式”迫使我避免将get_instance 和静态变量放在父类中,所以我不认为它是最佳解决方案。如果由于某种原因我的get_instance() 函数会改变将来,我不想编辑所有类(继承!继承!我们都想要继承!

那么,有没有办法或最佳实践来解决这个问题?

ps:php5.3.2

【问题讨论】:

标签: php oop design-patterns static singleton


【解决方案1】:

PHP 中的单例模式是这样的:

class Singleton {
     private static $instance = null;

     // Constructor is private, so class cannot be instantiazed from outside
     private function __construct() {
     }

     public static function getInstance() {
         if (static::$instance === null) {
              static::$instance = new Singleton();
         }
         return static::$instance;
     }

     public static function test() {
         echo 'Singleton::test()';
     }

     public function __sleep() {
         throw new Exception('Serialization is not alowed.');
     }

     public function __wakeup() {
         throw new Exception('Serialization is not alowed.');
     }

     public function __clone() {
         throw new Exception('Cloning is not alowed.');
     }
}

对你来说,关键字static很重要,那么这个:

class B extends Singleton {
    public static function test2() {
         echo 'B::test2()';
    }
}

$b = B::getInstance();
B::test();
B::test2();
// Singleton::test()
// B::test()

这是您要找的吗?

【讨论】:

  • 嗯,是的,我错过了new static();。谢谢伙计,你让我开心!
  • 要获得完整的答案,重要的是要说,后期静态绑定功能是 PHP 5.3 附带的,更多信息可以在这里找到:php.net/manual/en/language.oop5.late-static-bindings.php
  • @Jakub Truneček 有趣的回复:后期静态绑定,感谢您打开我的眼睛。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-07
  • 2021-06-24
  • 1970-01-01
相关资源
最近更新 更多