PHP Conference Japan 2024

ReflectionClass 類別

(PHP 5, PHP 7, PHP 8)

簡介

ReflectionClass 類別會回報關於類別的資訊。

類別概要

class ReflectionClass implements Reflector {
/* 常數 */
public const int IS_IMPLICIT_ABSTRACT;
public const int IS_FINAL;
public const int IS_READONLY;
public const int SKIP_DESTRUCTOR;
/* 屬性 */
public string $name;
/* 方法 */
public __construct(object|string $objectOrClass)
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 getEndLine(): int|false
公開 getInterfaces(): 陣列
公開 getMethods(?整數 $filter = null): 陣列
公開 getModifiers(): 整數
公開 getName(): 字串
公開 getProperties(?整數 $filter = null): 陣列
公開 取得反射常數(?int $filter = null): 陣列
公開 取得靜態屬性值(字串 $name, 混合 &$def_value = ?): 混合
公開 isEnum(): 布林值
公開 isFinal(): 布林值
公開 isInstance(物件 $object): 布林值
公開 isTrait(): 布林值
公開 newInstance(混合 ...$args): 物件
公開 newInstanceArgs(陣列 $args = []): ?物件
公開 newLazyGhost(可呼叫 $initializer, 整數 $options = 0): 物件
公開 newLazyProxy(可呼叫 $factory, 整數 $options = 0): 物件
公開 resetAsLazyGhost(物件 $object, 可呼叫 $initializer, 整數 $options = 0):
公開 resetAsLazyProxy(物件 $object, 可呼叫 $factory, 整數 $options = 0):
公開 setStaticPropertyValue(字串 $name, 混合 $value):
公開 __toString(): 字串
}

屬性

name

類別的名稱。唯讀,嘗試寫入會拋出 ReflectionException 例外。

預定義常數

ReflectionClass 修飾符

ReflectionClass::IS_IMPLICIT_ABSTRACT 整數

表示該類別因為有一些抽象方法而為抽象類別。

ReflectionClass::IS_EXPLICIT_ABSTRACT 整數

表示該類別因為其定義而為抽象類別。

ReflectionClass::IS_FINAL 整數

表示該類別為最終類別。

ReflectionClass::IS_READONLY 整數

表示該類別為唯讀類別。

ReflectionClass::SKIP_INITIALIZATION_ON_SERIALIZE int (整數)
指示 serialize() 不應觸發延遲物件的初始化。
ReflectionClass::SKIP_DESTRUCTOR int (整數)
指示在將物件重置為延遲物件時,不應呼叫物件的解構子。

更新日誌

版本 說明
8.4.0 類別常數現在已鍵入。
8.0.0 ReflectionClass::export() 已移除。

目錄

新增註解

使用者貢獻的註解 4 則註解

danbettles at yahoo dot co dot uk
15 年前
要在 PHP 5.3 中反映命名空間類別,您必須始終指定類別的完整名稱,即使您已使用「use」語句為包含的命名空間建立別名。

所以不要這樣

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

您應該輸入

<?php
use App\Core as Core;
$oReflectionClass = new ReflectionClass('App\Core\Singleton');
?>
匿名
13 年前
反序列化的反射類別會造成錯誤。

<?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 致命錯誤:ReflectionClass::getDocComment(): 內部錯誤:無法取得反射物件
?>
匿名
11 年前
反射別名會給你解析後類別的反射。

<?php

class X {

}

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

?>
To Top