ReflectionObject 类

(PHP 5, PHP 7)

简介

ReflectionObject 类报告了一个对象(object)的相关信息。

类摘要

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

属性

name

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

Table of Contents

User Contributed Notes

ccb_bc at hotmail dot com 21-May-2019 03:39
<?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());
kuldipem at gmail dot com 17-Jun-2015 06:01
class a {
    const abc = 1;
    public function __get($key){
        $r = new ReflectionObject($this);
        if($r->hasConstant($key)){ return $r->getConstant($key); }
    }
}
class b {
    public function getA(){
        return new a();
    }
}
$b = new b();
var_dump($b->getA()::abc);
var_dump($b->getA()::def);
marcel dot nolte at noltecomputer dot de 02-Sep-2009 02:42
To simply enlist all methods and properties of an object simply write:

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

,which will cause an var_export-like output.