是否有內置的靜態方法或屬性來引用PHP類,以便它將上下文表示爲字符串?例如:作爲字符串的PHP類名稱
取而代之的是:
$obj->tempFn('MyClass') //MyClass being the name of the class
我想這樣做:
$obj->tempFn(MyClass) //Directly references the class name, instead of a string representation
是否有內置的靜態方法或屬性來引用PHP類,以便它將上下文表示爲字符串?例如:作爲字符串的PHP類名稱
取而代之的是:
$obj->tempFn('MyClass') //MyClass being the name of the class
我想這樣做:
$obj->tempFn(MyClass) //Directly references the class name, instead of a string representation
echo get_class($this);
應的類的內部工作。
echo __CLASS__;
我相信這是一個靜態屬性
謝謝,但是我需要在課堂之外引用它,並且不要首先實例化這個類。 – astonius 2013-02-20 16:35:00
你可以返回' _靜態方法中的__CLASS__'屬性(即靜態函數getName())? – Husman 2013-02-20 16:37:58
@astonius這是一個玩笑嗎?如果你沒有實例化類,你只能通過靜態方法和變量來訪問它。類似'Math :: sum()'。只需手工寫'echo'Math''?? – vikingmaster 2013-02-20 16:38:40
如果真的想避開我覺得思考類可能的工作靜態。
function getClassName(ReflectionParameter $param) {
preg_match('/\[\s\<\w+?>\s([\w]+)/s', $param->__toString(), $matches);
return isset($matches[1]) ? $matches[1] : null;
}
從評論對http://www.php.net/manual/en/reflectionparameter.getclass.php
$ obj-> tempFn(新MyClass的()) 或 $ MyClass的=新MyClass的(); //用這個對象做點什麼 $ obj-> tempFn($ myClass); – sanj 2013-02-20 16:33:05
請注意,新的MyClass()會創建類MyClass的一個實例,它與對類的引用不一樣。但是,爲什麼你需要這樣做呢? – cernunnos 2013-02-20 16:34:43
請解釋你的意思是「上下文被表示爲一個字符串」。你想要做什麼?這個問題可以用幾種方式解釋。 – 2013-02-20 16:36:11