Reflection and usage of PHP variables and type extensions

Source: Internet
Author: User
Reflection and usage of PHP variables and type extensions

I. Overview and installation

PHP 5 has a complete reflection API and adds the reverse engineering capability for classes, interfaces, functions, methods, and extensions. In addition, the reflection API provides methods to retrieve document comments in functions, classes, and methods.

Note that some internal APIs lose the code required for reflection extension. For example, a built-in PHP class may lose the data of the reflection attribute. These minority cases are considered as errors. However, because of this, they should be discovered and repaired.

You do not need to install these functions. they are part of the PHP core.

II. example

There are many examples in the reflection document, usually in the _ construct document of each class.

A reflection Example (a terminal) in Example Shell)

$ Php -- rf strlen
$ Php -- rc finfo
$ Php -- re json
$ Php -- ri dom

The output of the preceding routine is similar:

Function [ 
 
   function strlen ] {  - Parameters [1] {    Parameter #0 [ 
  
    $str ]  }}Class [ 
   
     class finfo ] {  - Constants [0] {  }  - Static properties [0] {  }  - Static methods [0] {  }  - Properties [0] {  }  - Methods [4] {    Method [ 
    
      public method finfo ] {      - Parameters [2] {        Parameter #0 [ 
     
       $options ]        Parameter #1 [ 
      
        $arg ] } } Method [ 
       
         public method set_flags ] { - Parameters [1] { Parameter #0 [ 
        
          $options ] } } Method [ 
         
           public method file ] { - Parameters [3] { Parameter #0 [ 
          
            $filename ] Parameter #1 [ 
           
             $options ] Parameter #2 [ 
            
              $context ] } } Method [ 
             
               public method buffer ] { - Parameters [3] { Parameter #0 [ 
              
                $string ] Parameter #1 [ 
               
                 $options ] Parameter #2 [ 
                
                  $context ] } } }}Extension [ 
                 
                   extension #23 json version 1.2.1 ] { - Constants [10] { Constant [ integer JSON_HEX_TAG ] { 1 } Constant [ integer JSON_HEX_AMP ] { 2 } Constant [ integer JSON_HEX_APOS ] { 4 } Constant [ integer JSON_HEX_QUOT ] { 8 } Constant [ integer JSON_FORCE_OBJECT ] { 16 } Constant [ integer JSON_ERROR_NONE ] { 0 } Constant [ integer JSON_ERROR_DEPTH ] { 1 } Constant [ integer JSON_ERROR_STATE_MISMATCH ] { 2 } Constant [ integer JSON_ERROR_CTRL_CHAR ] { 3 } Constant [ integer JSON_ERROR_SYNTAX ] { 4 } } - Functions { Function [ 
                  
                    function json_encode ] { - Parameters [2] { Parameter #0 [ 
                   
                     $value ] Parameter #1 [ 
                    
                      $options ] } } Function [ 
                     
                       function json_decode ] { - Parameters [3] { Parameter #0 [ 
                      
                        $json ] Parameter #1 [ 
                       
                         $assoc ] Parameter #2 [ 
                        
                          $depth ] } } Function [ 
                         
                           function json_last_error ] { - Parameters [0] { } } }}domDOM/XML => enabledDOM/XML API Version => 20031129libxml Version => 2.7.3HTML Support => enabledXPath Support => enabledXPointer Support => enabledSchema Support => enabledRelaxNG Support => enabled
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 

III. related extensions

If you want to create a special version of the built-in class (for example, when creating and exporting highlighted HTML, you can replace the method with easy-to-access member variables or use practical methods ), you can continue and expand them.

Example #1 extend the built-in class

 Visibility = Reflection: getModifierNames ($ this-> getModifiers () ;}/ *** Demo class #1 **/class T {protected function x () {}}/*** Demo class #2 **/class U extends T {function x () {}} // output information var_dump (new My_Reflection_Method ('u ', 'X');?> The output of the preceding routine is similar to: object (My_Reflection_Method) #1 (3) {["visibility"] => array (1) {[0] => string (6) "public"} ["name"] => string (1) "x" ["class"] => string (1) "U "}

If you override the constructor, remember to call the constructor of the parent class before writing any inserted code. Otherwise, the following results will be returned: Fatal error: Internal error: Failed to retrieve the reflection object

IV. reflection

Reflection-Reflection class

ReflectionClass-ReflectionClass class

ReflectionZendExtension-ReflectionZendExtension class

ReflectionExtension-ReflectionExtension class

ReflectionFunction-ReflectionFunction class

ReflectionFunctionAbstract-ReflectionFunctionAbstract class

ReflectionMethod-ReflectionMethod class

ReflectionObject-ReflectionObject class

ReflectionParameter-ReflectionParameter class

ReflectionProperty-ReflectionProperty class

Reflector-Reflector interface

ReflectionException-ReflectionException class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.