略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: ReflectionClass

2024-11-13

ReflectionClass 类

(PHP 5, PHP 7, PHP 8)

简介

ReflectionClass 类报告了一个类的有关信息。

类摘要

class ReflectionClass implements Reflector {
/* 常量 */
const integer IS_IMPLICIT_ABSTRACT = 16;
const integer IS_EXPLICIT_ABSTRACT = 32;
const integer IS_FINAL = 64;
/* 属性 */
public $name;
/* 方法 */
public __construct(mixed $argument)
public static export(mixed $argument, bool $return = false): string
public getAttributes(?string $name = null, int $flags = 0): array
public getConstant(string $name): mixed
public getConstants(?int $filter = null): array
public getDefaultProperties(): array
public getDocComment(): string
public getEndLine(): int
public getExtensionName(): string
public getFileName(): string
public getInterfaceNames(): array
public getInterfaces(): array
public getMethod(string $name): ReflectionMethod
public getMethods(int $filter = ?): array
public getModifiers(): int
public getName(): string
public getNamespaceName(): string
public getProperties(int $filter = ?): array
public getProperty(string $name): ReflectionProperty
public getReflectionConstants(?int $filter = null): array
public getShortName(): string
public getStartLine(): int
public getStaticProperties(): array
public getStaticPropertyValue(string $name, mixed &$def_value = ?): mixed
public getTraitAliases(): array
public getTraitNames(): array
public getTraits(): array
public hasConstant(string $name): bool
public hasMethod(string $name): bool
public hasProperty(string $name): bool
public implementsInterface(string $interface): bool
public inNamespace(): bool
public isAbstract(): bool
public isAnonymous(): bool
public isCloneable(): bool
public isEnum(): bool
public isFinal(): bool
public isInstance(object $object): bool
public isInstantiable(): bool
public isInterface(): bool
public isInternal(): bool
public isIterable(): bool
public isIterateable(): bool
public isSubclassOf(string $class): bool
public isTrait(): bool
public isUserDefined(): bool
public newInstance(mixed $args, mixed $... = ?): object
public newInstanceArgs(array $args = ?): object
public setStaticPropertyValue(string $name, string $value): void
public __toString(): string
}

属性

name

类的名称。只读,并在尝试赋值的时候会抛出 ReflectionException

预定义常量

ReflectionClass 修饰符

ReflectionClass::IS_IMPLICIT_ABSTRACT

指示了类是一个抽象类(abstract), 因为它有抽象(abstract)方法。

ReflectionClass::IS_EXPLICIT_ABSTRACT

指示了类是一个抽象类(abstract), 因为它已明确定义。

ReflectionClass::IS_FINAL

指示这是一个 final 类。

目录

add a noteadd a note

User Contributed Notes 5 notes

up
36
danbettles at yahoo dot co dot uk
13 years ago
To reflect on a namespaced class in PHP 5.3, you must always specify the fully qualified name of the class - even if you've aliased the containing namespace using a "use" statement.

So instead of:

<?php
use App\Core as Core;
$oReflectionClass = new ReflectionClass('Core\Singleton');
?>

You would type:

<?php
use App\Core as Core;
$oReflectionClass = new ReflectionClass('App\Core\Singleton');
?>
up
19
Anonymous
9 years ago
Reflecting an alias will give you a reflection of the resolved class.

<?php

class X {
   
}

class_alias('X','Y');
class_alias('Y','Z');
$z = new ReflectionClass('Z');
echo
$z->getName(); // X

?>
up
23
Anonymous
10 years ago
Unserialized reflection class cause error.

<?php
/**
* abc
*/
class a{}

$ref = new ReflectionClass('a');
$ref = unserialize(serialize($ref));
var_dump($ref);
var_dump($ref->getDocComment());

// object(ReflectionClass)#2 (1) {
//   ["name"]=>
//   string(1) "a"
// }
// PHP Fatal error:  ReflectionClass::getDocComment(): Internal error: Failed to retrieve the reflection object
?>
up
2
featherbits
1 year ago
In order to get class attributes look here (php8)
https://www.php.net/manual/en/language.attributes.reflection.php
up
-32
YoungOfCthulhu
6 years ago
It is also possible to do reflection from within the class and get the methods for instance.

<?php

class test
{
    private
$test = "";

    public function
setTest($test)
    {
       
$this->test = $test;
        return
$this;
    }

    public function
getTest()
    {
        return
$this->test;
    }
   
    public function
run(){
       
$class = new ReflectionClass('test');
       
$methods = $class->getMethods();
       
var_dump($methods);
    }
}

Called from another php file you get the proper result:
array(
3) {
  [
0]=>
  &
object(ReflectionMethod)#3 (2) {
   
["name"]=>
   
string(8) "setTest"
   
["class"]=>
   
string(4) "test"
 
}
  [
1]=>
  &
object(ReflectionMethod)#4 (2) {
   
["name"]=>
   
string(8) "getTest"
   
["class"]=>
   
string(4) "test"
 
}
  [
2]=>
  &
object(ReflectionMethod)#5 (2) {
   
["name"]=>
   
string(3) "run"
   
["class"]=>
   
string(4) "test"
 
}
}

官方地址:https://www.php.net/manual/en/class.reflectionclass.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3