实现静态类继承的最佳实践?(singleton)


Best practice to implement static class inheritance? (singleton)

From php manual:

[…静态方法调用在编译时解析。当使用显式类名时,方法已经完全被识别出来了继承规则适用。如果调用是由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()函数将来会改变,我不想编辑所有的类(继承!)继承!我们都想继承!)

那么,有没有解决这个问题的方法或最佳实践呢?

p。s: php5.3.2

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()

你在找这个吗?