2011-06-29 17 views
5
<?php 
class MyParent { 

    public static function tellSomething() { 
     return __CLASS__; 
    } 
} 

class MyChild extends MyParent { 

} 

echo MyChild::tellSomething(); 

上記のコードは "MyParent"です。子クラスの名前を取得するにはどうすればいいですか?この場合は "MyChild"ですか?可能であれば...静的な子メソッドからクラス名を取得する方法

どの子が継承されたメソッドを呼び出すのかを知るだけでいいです。

+1

可能な重複(追記として通常の方法は、彼らが呼ばれていた上のクラスを知っている必要はありません)[Iは、拡張PHPクラスの静的コールからのクラス名を取得できますか?](https://stackoverflow.com/questions/506705/how-can-i-get-the-classname-from-a-static-call-in-an-extended-php-class) – Theraot

答えて

7

__CLASS__は疑似定数で、常に定義されているクラスを参照します。 late-static-bindingでは、実行時にクラス名を解決する関数get_called_class()が導入されました。

class MyParent { 

    public static function tellSomething() { 
    return get_called_class(); 
    } 
} 

class MyChild extends MyParent { 

} 

echo MyChild::tellSomething(); 

:の

5

説明しているものはLate Static Bindingsと呼ばれ、PHP 5.3で利用可能になりました。

関連する問題