在插件開發中,PHP ReflectionClass 可以幫助你動態地獲取類的信息、方法和屬性。以下是一些常見的用法:
$reflectionClass = new ReflectionClass('YourClassName');
$className = $reflectionClass->getName();
echo $className; // 輸出 "YourClassName"
$reflectionClass = new ReflectionClass('YourClassName');
$methods = $reflectionClass->getMethods();
foreach ($methods as $method) {
echo $method->getName() . "\n";
}
$reflectionClass = new ReflectionClass('YourClassName');
$properties = $reflectionClass->getProperties();
foreach ($properties as $property) {
echo $property->getName() . "\n";
}
$reflectionClass = new ReflectionClass('YourClassName');
$interfaceNames = $reflectionClass->getInterfaces();
foreach ($interfaceNames as $interfaceName) {
echo $interfaceName . "\n";
}
$reflectionClass = new ReflectionClass('YourClassName');
$constructor = $reflectionClass->getConstructor();
if ($constructor) {
$instance = $reflectionClass->newInstance();
} else {
$instance = $reflectionClass->newInstanceWithoutConstructor();
}
$reflectionClass = new ReflectionClass('YourClassName');
$method = $reflectionClass->getMethod('methodName');
$method->invoke($instance, $arg1, $arg2, ...);
在插件開發中,你可以使用這些方法來動態地操作類和對象,從而提高代碼的可維護性和擴展性。