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

时间:2011-07-14 09:23:27

标签: php oop design-patterns static singleton

来自php手册:

  

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

..所以我正在寻找一种方法来模仿标准 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和静态变量放在父类中,因此我不认为它是最佳解决方案..如果由于某种原因我的{{1}函数将来会改变,我不想编辑所有类(继承!继承!我们都想继承!

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

p.s:php5.3.2

1 个答案:

答案 0 :(得分:4)

PHP中的Singleton模式是这样的:

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

这是你要找的吗?