PHP 2 Kinds of Reflection class Reflectionclass, Reflectionmethod usage detailed

Source: Internet
Author: User
Tags php language
This article mainly introduced the PHP reflection class Reflectionclass, Reflectionmethod Use example, this article also introduced the reflection is what, reflection can do what things, and give a concrete example, is a good introductory article, the need for friends can refer to the next

The PHP5 has a complete reflection API that adds the ability to reverse engineer classes, interfaces, functions, methods, and extensions.

What is reflection?

It refers to the PHP running state, extending the parsing PHP program, exporting or extracting details about classes, methods, properties, parameters, etc., including comments. This dynamic acquisition of information and the ability to dynamically invoke an object's methods is called the Reflection API. Reflection is an API that manipulates the meta-model of an object-oriented paradigm and is powerful in helping us to build complex, scalable applications.

The purpose is to automatically load plugins, automatically generate documents, and even to augment the PHP language.

The PHP reflection API consists of several classes that help us to access the program's metadata or to interact with the related annotations. With reflection we can obtain methods such as class implementations, create an instance of a class (unlike the one created with new), Invoke a method (also different from a regular call), pass a parameter, and invoke the static method of the class dynamically.
The Reflection API is a PHP built-in OOP technology extension that includes classes, exceptions, and interfaces that are used in combination to help us analyze other classes, interfaces, methods, properties, methods, and extensions. These OOP extensions are known as reflections.

Usually we use more of the Reflectionclass class and the Reflectionmethod class, for example:

The code is as follows:

<?phpclass person {/**  * For the sake of demonstration, we ' re setting this private  */Private $_allowdynamicat Tributes = false; /**  * type=primary_autoincrement * */  protected $id = 0;/**  * Type=varchar length=255 NULL  */protected $n Ame  /**  * Type=text null *  /protected $biography; public Function getId () {  return $this->id;} public function SetId ($v) {  $this->id = $v;} public Function GetName () {  return $this->name;} public Function SetName ($v {  $this->name = $v;} public Function Getbiography () {  return $this->biography;} public Function Setbio Graphy ($v) {  $this->biography = $v;}}

First, through Reflectionclass, we can get the following information of the person class:

1. Constant contants
2. Property Names
3. Method names static
4. Property Static Properties
5. Namespace Namespace
Whether the 6.Person class is final or abstract
Does the 7.Person class have a method

Next reflect it, as long as the class name "person" is passed to Reflectionclass, you can:

The code is as follows:

$class = new Reflectionclass (' person '); The reflection class of this class is established  $instance  = $class->newinstanceargs ($args);//the equivalent of instantiating the person class

1) Get Attributes (properties):

The code is as follows:

$properties = $class->getproperties (); foreach ($properties as $property) {echo $property->getname (). "\ n";} Output://_allowdynamicattributes//id//name//biography

By default, Reflectionclass will get all of the properties, private and protected can also. If you want to get only the private property, you need to pass an additional parameter:

The code is as follows:

$private _properties = $class->getproperties (reflectionproperty::is_private);

List of available parameters:

The code is as follows:

Reflectionproperty::is_staticreflectionproperty::is_publicreflectionproperty::is_protectedreflectionproperty: : Is_private


The property name can be obtained by $property->getname ().

2) Get Comments:

A note to the property can be obtained by getdoccomment.

The code is as follows:

foreach ($properties as $property) {if ($property->isprotected ()) {  $docblock = $property->getdoccomment (); C3/>preg_match ('/type\= ([a-z_]*)/', $property->getdoccomment (), $matches);  echo $matches [1]. "\ n"; }}//output://primary_autoincrement//varchar//text

3) methods to get classes

The code is as follows:

GetMethods ()       to get all the methods to the class. Hasmethod (String)  whether there is a method GetMethod (string)  get method

4) Method of executing the class:

The code is as follows:

$instance->getname (); Execute the method in person getname//or: $method = $class->getmethod (' getName '); Gets the GetName method in the Person class $method->invoke ($instance);    Execute the GetName method//or: $method = $class->getmethod (' setName '); Gets the SetName method in the Person class $method->invokeargs ($instance, Array (' snsgou.com '));

Second, through Reflectionmethod, we can get information about a method of the person class:

1. Whether "public", "protected", "private", "static" type
2. Parameter list of the method
3. Number of parameters for the method
4. Methods of sung using classes

The code is as follows:

Execute Detail Method $method = new Reflectionmethod (' person ', ' test '); if ($method->ispublic () &&! $method IsStatic ()) {echo ' Action is Right ';} echo $method->getnumberofparameters (); Number of parameters echo $method->getparameters (); Array of Parameter objects

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.