反射API 涉及的類
- Reflection
- ReflectionClass
- ReflectionZendExtension
- ReflectionExtension
- ReflectionFunction
- ReflectionFunctionAbstract
- ReflectionMethod
- ReflectionObject
- ReflectionParameter
- ReflectionProperty
ReflectionClass 分析
作用是報告一類的有關(guān)信息。
export
用于輸出一個類。
示例:
class User{}
ReflectionClass::export('User');
輸出:
Class [ class User ] {
@@ D:\path\test.php 3-5
- Constants [0] { }
- Static properties [0] { }
- Static methods [0] { }
- Properties [0] { }
- Methods [0] { } }
hasMothed
用于判斷是否擁有該方法。
getMothed
獲取一個類方法的 ReflectionMethod 。
invoke
用于執(zhí)行這個方法。
ReflectionMethod 分析
作用是報告了一個方法的有關(guān)信息。
invoke && invokeArgs
作用是執(zhí)行一個類的方法。
invoke和invokeArgs的區(qū)別是。
示例:
class User{
public function sayHello(){
echo 'Hello';
}
}
$reflectionMethod = new ReflectionMethod('User','sayHello');
$reflectionMethod->invoke(new User());
isXxx 方法
以下代碼,下面子章節(jié)公用:
class User{
public function sayHello(){
echo 'Hello';
}
}
$reflectionMethod = new ReflectionMethod('User','sayHello');
isPublic
用于判斷方法是否是公開方法。
$reflectionMethod->isPublic(); // TRUE
isStatic
用于判斷是否是靜態(tài)方法。
$reflectionMethod->isStaic() // FALSE