來自PHP的ReflectionMethod
實例(http://php.net/manual/en/class.reflectionmethod.php)具有返回方法註釋的getDocComment
方法。這工作正常,除非你使用反序列化的對象。PHP反編譯後的Doc文檔註釋
$ref = new ReflectionClass('a');
var_dump(method_exists($ref, 'getDocComment')); //bool(true)
var_dump($ref->getDocComment()); //bool(false)
$ref = unserialize(serialize($ref));
var_dump(method_exists($ref, 'getDocComment')); //bool(true)
var_dump($ref->getDocComment()); //PHP Warning: Uncaught Error: Internal error: Failed to retrieve the reflection object
如果ReflectionMethod
對象已正確定義文檔註釋,是否有任何測試方法?我的意思是,我不在乎在serialize/unserialize之後獲得註釋,但我想檢查是否調用getDocComment
是安全的。
編輯:根據他的意見錯誤處理+備用,我改寫了Q.
的回答,我有思考的一些簡單的緩存(ReflectionMethod
對象數組)。在我使用該緩存中的項目之前,我希望能夠正確處理它。我不想處理錯誤,我想「預測錯誤」。太棒了,就像hasDocComment
方法不會產生任何錯誤,但在任何ReflectionMethod
對象狀態中只返回true/false。
不會getDocComment返回false它不存在? – Erik
@Erik是的,它會 - 看代碼的第一部分 – Jarda