從php手冊:實現靜態類繼承的最佳實踐? (singleton)
[...]靜態方法調用在編譯時解析。 使用明確的類名稱時,該方法已被完全標識,並且不會應用繼承規則。如果通話是通過自我完成的,那麼self會被轉換爲當前類別 ,即代碼所屬的類別。 這裏也沒有繼承規則[...]
..所以尋找一種方式來IM效仿的標準接力傳承與靜態單。
代碼更好地解釋:
// 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
[不要使用單身人士](http://googletesting.blogspot.com/2008/08/by-miko-hevery-so-you-join-new-project.html) –
*(相關)* [誰需要單身人士](http://stackoverflow.com/questions/4595964/who-needs-singletons/4596323#4596323) – Gordon
*(相關)* [靜態認爲有害](http://kore-nordmann.de/blog /0103_static_considered_harmful.html) – Gordon