Home > Backend Development > PHP Tutorial > PHP Advanced Programming (2)_PHP Tutorial

PHP Advanced Programming (2)_PHP Tutorial

WBOY
Release: 2016-07-13 10:28:14
Original
932 people have browsed it

PHP 5 features a full reflection API, adding the ability to reverse engineer classes, interfaces, functions, methods, and extensions. Additionally, the Reflection API provides methods to extract documentation comments from functions, classes, and methods. Other classes, interfaces, methods, properties, functions, and extensions can be analyzed using the reflection API. The following example shows the definition of PHP's own class Reflection:

Reflection::export(<span>new</span> ReflectionClass(<span>'</span><span>Reflection</span><span>'</span>));
Copy after login

The printing results are as follows:

Class [ <<span>internal</span>:Reflection> <span>class</span><span> Reflection ] {

  </span>- Constants [<span>0</span><span>] {
  }

  </span>- Static properties [<span>0</span><span>] {
  }

  </span>- Static methods [<span>2</span><span>] {
    Method [ </span><<span>internal</span>:Reflection> <span>static</span> <span>public</span><span> method getModifierNames ] {

      </span>- Parameters [<span>1</span><span>] {
        Parameter #</span><span>0</span> [ <required><span> $modifiers ]
      }
    }

    Method [ </span><<span>internal</span>:Reflection> <span>static</span> <span>public</span><span> method export ] {

      </span>- Parameters [<span>2</span><span>] {
        Parameter #</span><span>0</span> [ <required><span> Reflector $reflector ]
        Parameter #</span><span>1</span> [ <optional> $<span>return</span><span> ]
      }
    }
  }

  </span>- Properties [<span>0</span><span>] {
  }

  </span>- Methods [<span>0</span><span>] {
  }
}</span>
Copy after login

It can be seen from the printing results that export is a public static method of the Reflection class, providing two parameters, one must be provided as reflector type, and the other is optional, bool type. Not only export, there are many reflection APIs. Through these APIs, we can also query the metadata of the class and dynamically call the static methods of the class. Reflection can also be used to create an automatic and stable writing system. The reflection API defines a large number of is and has type functions, which can be used to perform conditional judgments in code. For example, use isUserDefined() to determine which of the loaded classes are user-defined.

(To be continued)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/812981.htmlTechArticlePHP 5 has a complete reflection API, adding reverse engineering of classes, interfaces, functions, methods and extensions ability. Additionally, the Reflection API provides methods to retrieve functions, classes, and methods...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template