在PHP中,可以使用ReflectionClass
類來獲取類的信息。ReflectionClass
類提供了許多方法來檢查類的屬性和方法。以下是一些常用的方法:
$reflectionClass = new ReflectionClass('ClassName');
$className = $reflectionClass->getName();
echo $className; // 輸出 "ClassName"
if ($reflectionClass->isClass()) {
echo "Class exists.";
} else {
echo "Class does not exist.";
}
$reflectionClass = new ReflectionClass('ClassName');
$documentationComment = $reflectionClass->getDocComment();
echo $documentationComment;
$reflectionClass = new ReflectionClass('ClassName');
$properties = $reflectionClass->getProperties();
foreach ($properties as $property) {
echo "Property name: " . $property->getName() . "\n";
echo "Property type: " . $property->getType() . "\n";
echo "Property default value: " . $property->getDefaultValue() . "\n";
}
$reflectionClass = new ReflectionClass('ClassName');
$methods = $reflectionClass->getMethods();
foreach ($methods as $method) {
echo "Method name: " . $method->getName() . "\n";
echo "Method visibility: " . ($method->isPublic() ? "public" : ($method->isPrivate() ? "private" : "protected")) . "\n";
echo "Method return type: " . ($method->hasReturnType() ? $method->getReturnType() : "void") . "\n";
}
$reflectionClass = new ReflectionClass('ClassName');
$instance = $reflectionClass->newInstance();
$reflectionClass = new ReflectionClass('ClassName');
if ($reflectionClass->isInterface()) {
echo "Class is an interface.";
} elseif ($reflectionClass->isClass()) {
echo "Class is a class.";
} elseif ($reflectionClass->isTrait()) {
echo "Class is a trait.";
}
這些方法只是ReflectionClass
類提供的一部分功能。你可以根據需要使用這些方法來獲取類的詳細信息。