PHP Conference Japan 2024

ReflectionObject 類別

(PHP 5, PHP 7, PHP 8)

簡介

ReflectionObject 類別會回報關於 物件 的資訊。

類別概要

class ReflectionObject extends ReflectionClass {
/* 繼承的常數 */
/* 繼承的屬性 */
public string $name;
/* 方法 */
public __construct(object $object)
/* 繼承的方法 */
public static ReflectionClass::export(mixed $argument, bool $return = false): string
public ReflectionClass::getAttributes(?string $name = null, int $flags = 0): array
public ReflectionClass::getProperties(?int $filter = null): array

公開方法 ReflectionClass::getProperties( ?int $filter = null ): array

public ReflectionClass::getProperty(string $name): ReflectionProperty

公開方法 ReflectionClass::getProperty( string $name ): ReflectionProperty

public ReflectionClass::getReflectionConstant(string $name): ReflectionClassConstant|false

公開方法 ReflectionClass::getReflectionConstant( string $name ): ReflectionClassConstant|false

public ReflectionClass::getReflectionConstants(?int $filter = null): array

公開方法 ReflectionClass::getReflectionConstants( ?int $filter = null ): array

public ReflectionClass::getShortName(): string

公開方法 ReflectionClass::getShortName(): string

public ReflectionClass::getStartLine(): int|false

公開方法 ReflectionClass::getStartLine(): int|false

public ReflectionClass::getStaticProperties(): array

公開方法 ReflectionClass::getStaticProperties(): array

public ReflectionClass::getStaticPropertyValue(string $name, mixed &$def_value = ?): mixed

公開方法 ReflectionClass::getStaticPropertyValue( string $name, mixed &$def_value = ? ): mixed

public ReflectionClass::getTraitAliases(): array

公開方法 ReflectionClass::getTraitAliases(): array

public ReflectionClass::getTraitNames(): array

公開方法 ReflectionClass::getTraitNames(): array

public ReflectionClass::getTraits(): array

公開方法 ReflectionClass::getTraits(): array

public ReflectionClass::hasConstant(string $name): bool

公開方法 ReflectionClass::hasConstant( string $name ): bool

public ReflectionClass::hasMethod(string $name): bool

公開方法 ReflectionClass::hasMethod( string $name ): bool

公開 ReflectionClass::newLazyGhost(可呼叫 $initializer, 整數 $options = 0): 物件
公開 ReflectionClass::newLazyProxy(可呼叫 $factory, 整數 $options = 0): 物件
公開 ReflectionClass::resetAsLazyGhost(物件 $object, 可呼叫 $initializer, 整數 $options = 0):
公開 ReflectionClass::resetAsLazyProxy(物件 $object, 可呼叫 $factory, 整數 $options = 0):
}

更新日誌

版本 說明
8.0.0 ReflectionObject::export() 已移除。

目錄

新增筆記

使用者貢獻的筆記 2 則筆記

marcel dot nolte at noltecomputer dot de
15 年前
要簡單列出物件的所有方法和屬性,只需編寫

<?php ReflectionObject::export($yourObject); ?>

,這將會產生類似 var_export 的輸出。
ccb_bc at hotmail dot com
5 年前
<?php
class FullReflectionObject {

/**
* @var obj => $ReflectionObject = propriedade que representa o objeto $ReflectionObject
* @var obj => $ReflectionProperty = propriedade que representa o objeto $ReflectionProperty
* @var obj => $ReflectionMethod = propriedade que representa o objeto $ReflectionMethod
* @var obj => $ReflectionParameter = propriedade que representa o objeto $ReflectionParameter
* @var bol => $reflect_object = true se o método object() for invocado
* @var bol => $reflect_property = true se o método property() for invocado
* @var bol => $reflect_method = true se o método method() for invocado
* @var bol => $reflect_parameter = true se o método parameter() for invocado
*/

private $ReflectionObject;
private
$ReflectionProperty;
private
$ReflectionMethod;
private
$ReflectionParameter;
private
$reflect_object = false;
private
$reflect_property = false;
private
$reflect_method = false;
private
$reflect_parameter = false;

/**
* @param obj => $object = objeto a ser refletido
*/
public function object(object $object) : object {
$this->reflect_object = true;
$this->ReflectionObject = new \ReflectionObject($object);
return
$this;
}

/**
* @param str => $class_name = nome da classe o qual a properiedade ser refletida pertence
* @param str => $property = nome da properiedade a ser refletida
*/
public function property(string $class_name, string $property) : object {
$this->reflect_property = true;
$this->ReflectionProperty = new \ReflectionProperty($class_name, $property);
return
$this;
}

/**
* @param str => $class_name = nome da classe o qual o método ser refletido pertence
* @param str => $method = nome do método a ser refletido
*/
public function method(string $class_name, string $method) : object {
$this->reflect_method = true;
$this->ReflectionMethod = new \ReflectionMethod($class_name, $method);
return
$this;
}

/**
* @param str => $class_name = nome da classe o qual o parametro a ser refletido pertence
* @param str => $method = nome do método o qual o parametro a ser refletido pertence
* @param str => $parameter = nome do parametro a ser refletido
*/
public function parameter(string $class_name, string $method, string $parameter) : object {
$this->reflect_parameter = true;
$this->ReflectionParameter = new \ReflectionParameter(array($class_name, $method), $parameter);
return
$this;
}

public function
__call($methodName, $args){
if(
$this->reflect_object){
return
call_user_func_array(array($this->ReflectionObject, $methodName), $args);
}
if(
$this->reflect_property){
return
call_user_func_array(array($this->ReflectionProperty, $methodName), $args);
}
if(
$this->reflect_method){
return
call_user_func_array(array($this->ReflectionMethod, $methodName), $args);
}
if(
$this->reflect_parameter){
return
call_user_func_array(array($this->ReflectionParameter, $methodName), $args);
}
}
}

// Digamos que você tenha no seu projeto a classe Client do pacote Guzzle
$FullReflectionObject = new FullReflectionObject;
var_dump($FullReflectionObject->object(new GuzzleHttp\Client)->getMethods());
var_dump($FullReflectionObject->property('GuzzleHttp\Client', 'config')->isPrivate());
var_dump($FullReflectionObject->method('GuzzleHttp\Client', 'buildUri')->isPrivate());
var_dump($FullReflectionObject->parameter('GuzzleHttp\Client', 'send', 'options')->getDefaultValue());
To Top