Reflection--映像

    技术2026-04-25  7

    Reflection--映像

    Table of Contents

    内容目录

    Introduction 介绍 The Reflector interface 映射接口 The ReflectionException class 映射例外类 The ReflectionFunction class 映射方法类 The ReflectionParameter class 映射参数类 The ReflectionClass class 映射基本类 The ReflectionObject class 映射对象类 The ReflectionMethod class 映射方法类 The ReflectionProperty class 映射属性类 The ReflectionExtension class 映射扩展类 Extending the reflection classes 扩展映射类

    Introduction

    介绍

     

           PHP 5 comes with a complete reflection API that adds the ability to reverse-engineer classes, interfaces, functions and methods as well as extensions. Additionally, the reflection API also offers ways of retrieving doc comments for functions, classes and methods.

           PHP 5 提供了一个完整的反转类、接口、函数和方法还有扩展类的API.映射API还提供了为函数、类和方法弥补注释文档的方法。

           The reflection API is an object-oriented extension to the Zend Engine, consisting of the following classes:

           映射API是一个对Zend引擎的面向对象的扩展,看一下下面的类:

           <?php class Reflection { } interface Reflector { } class ReflectionException extends Exception { } class ReflectionFunction extends ReflectionFunctionAbstract implements Reflector { } class ReflectionParameter implements Reflector { } class ReflectionMethod extends ReflectionFunctionAbstract implements Reflector { } class ReflectionClass implements Reflector { } class ReflectionObject extends ReflectionClass { } class ReflectionProperty implements Reflector { } class ReflectionExtension implements Reflector { } ?>

     

     

    Note : For details on these classes, have a look at the next chapters.

    注意:对与这些类的详细解释,需要看下一篇文章

     

    If we were to execute the code in the example below:

    如果我们执行了下面的代码:

     

    <?php Reflection::export(new ReflectionClass('Exception')); ?>

    将会输出:

    Class [ <internal> class Exception ] { - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [6] { Property [ <default> protected $message ] Property [ <default> private $string ] Property [ <default> protected $code ] Property [ <default> protected $file ] Property [ <default> protected $line ] Property [ <default> private $trace ] } - Methods [9] { Method [ <internal> final private method __clone ] { } Method [ <internal, ctor> <visibility error> method __construct ] { - Parameters [2] { Parameter #0 [ <optional> $message ] Parameter #1 [ <optional> $code ] } } Method [ <internal> final public method getMessage ] { } Method [ <internal> final public method getCode ] { } Method [ <internal> final public method getFile ] { } Method [ <internal> final public method getLine ] { } Method [ <internal> final public method getTrace ] { } Method [ <internal> final public method getTraceAsString ] { } Method [ <internal> public method __toString ] { } } }

     

    Reflector

    映射器(反射器)

    Reflector is an interface implemented by all exportable Reflection classes.

    映射器是所有映射类输出的一个接口实现

     

    ReflectionException

    映射例外

    ReflectionException extends the standard Exception and is thrown by Reflection API. No specific methods or properties are introduced.

    映射例外是由映射API抛出的对标准例外的扩展。没有引入具体的方法和属性。

    ReflectionFunction

    映射函数

    The ReflectionFunction class lets you reverse-engineer functions.

    映射函数类让你有了反转引擎函数。

     

     

    To introspect a function, you will first have to create an instance of the ReflectionFunction class. You can then call any of the above methods on this instance.

    为了内省一个函数,首先你需要去建立一个映射类的实例。然后你就可以调用这个实例里的任何方法了。

     

    Using the ReflectionFunction class <?php /** * A simple counter * * @return int */ function counter() { static $c = 0; return $c++; } // Create an instance of the ReflectionFunction class $func = new ReflectionFunction('counter'); // Print out basic information printf( "===> The %s function '%s'/n". " declared in %s/n". " lines %d to %d/n", $func->isInternal() ? 'internal' : 'user-defined', $func->getName(), $func->getFileName(), $func->getStartLine(), $func->getEndline() ); // Print documentation comment printf("---> Documentation:/n %s/n", var_export($func->getDocComment(), 1)); // Print static variables if existant if ($statics = $func->getStaticVariables()) { printf("---> Static variables: %s/n", var_export($statics, 1)); } // Invoke the function printf("---> Invokation results in: "); var_dump($func->invoke()); // you may prefer to use the export() method echo "/nReflectionFunction::export() results:/n"; echo ReflectionFunction::export('counter'); ?>

     

     

     

    最新回复(0)