【发布时间】: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
【问题讨论】:
-
(相关) Who needs Singletons
-
感谢您的链接,我会仔细看看我的设计。
标签: php oop design-patterns static singleton