共計 872 個字符,預計需要花費 3 分鐘才能閱讀完成。
在 PHP 中,可以使用反射 API 來獲取類的信息。以下是一個簡單的示例代碼:
class MyClass {public $prop1;
protected $prop2;
private $prop3;
public function method1() {// method code
}
protected function method2() {// method code
}
private function method3() {// method code
}
}
$reflectionClass = new ReflectionClass('MyClass');
echo 'Class name: ' . $reflectionClass->getName() . "\n";
$properties = $reflectionClass->getProperties();
echo 'Properties: ';
foreach ($properties as $property) {echo $property->getName() . ', ';
}
echo "\n";
$methods = $reflectionClass->getMethods();
echo 'Methods: ';
foreach ($methods as $method) {echo $method->getName() . ', ';
}
echo "\n";
// 輸出結果:
// Class name: MyClass
// Properties: prop1, prop2, prop3,
// Methods: method1, method2, method3,
在上面的示例中,首先創建了一個 MyClass
類。然后使用 ReflectionClass
來獲取類的信息,包括類名、屬性和方法。通過調用 getName()
方法獲取類名,getProperties()
方法獲取屬性列表,getMethods()
方法獲取方法列表。最后分別輸出類名、屬性和方法的信息。
丸趣 TV 網 – 提供最優質的資源集合!
正文完