This example introduces the Reflection mechanism of PHP.

Source: Internet
Author: User
This article mainly introduces the Reflection mechanism of PHP. This article introduces the Reflection mechanism of PHP from the perspective of getting a class information using Reflection, you can refer to PHP5 to add a new function: Reflection. This function allows programmers to reverse-engineer class, interface, function, method and extension. With PHP code, you can obtain all the information of an object and interact with it.
Assume there is a class Person:

The code is as follows:


Class Person {
/**
* For the sake of demonstration, we "re setting this private
*/
Private $ _ allowDynamicAttributes = false;

/** Type = primary_autoincrement */
Protected $ id = 0;

/** Type = varchar length = 255 null */
Protected $ name;

/** 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 setBiography ($ v)
{
$ This-> biography = $ v;
}
}

Through ReflectionClass, we can get the following information about the Person class:
1. constant Contants
2. Property Names
3. Method Names
4. Static Properties
5. Namespace
6. whether the Person class is final or abstract

You only need to pass the class name "Person" to ReflectionClass:

The code is as follows:


$ Class = new ReflectionClass ('person ');

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 obtains all attributes, and private and protected attributes can also be obtained. If you only want to get 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_STATIC
ReflectionProperty: IS_PUBLIC
ReflectionProperty: IS_PROTECTED
ReflectionProperty: IS_PRIVATE

If you want to obtain both the public and private attributes, write: ReflectionProperty: IS_PUBLIC | ReflectionProperty: IS_PROTECTED.
It should be no stranger.

You can get the property name through $ property-> getName (), and get the comment to the property through getDocComment.

The code is as follows:


Foreach ($ properties as $ property ){
If ($ property-> isProtected ()){
$ Docblock = $ property-> getDocComment ();
Preg_match ('/type \ = ([a-z _] *)/', $ property-> getDocComment (), $ matches );
Echo $ matches [1]. "\ n ";
}
}
// Output:
// Primary_autoincrement
// Varchar
// Text

That's incredible. Even comments can be obtained.
Method: getMethods () is used to obtain all the methods of the class. The returned result is an array of reflemethod method objects. Do not demo any more.
Finally, the ReflectionMethod is used to call the method in the class.

The code is as follows:


$ Data = array ("id" => 1, "name" => "Chris", "biography" => "I am a PHP developer ");
Foreach ($ data as $ key => $ value ){
If (! $ Class-> hasProperty ($ key )){
Throw new Exception ($ key. "is not a valid property ");
}

If (! $ Class-> hasMethod ("get". ucfirst ($ key ))){
Throw new Exception ($ key. "is missing a getter ");
}

If (! $ Class-> hasMethod ("set". ucfirst ($ key ))){
Throw new Exception ($ key. "is missing a setter ");
}

// Make a new object to interact
$ Object = new Person ();

// Get the getter method and invoke it with the value in our data array
$ Setter = $ class-> getMethod ("set". ucfirst ($ key ));
$ OK = $ setter-> invoke ($ object, $ value );

// Get the setter method and invoke it
$ Setter = $ class-> getMethod ("get". ucfirst ($ key ));
$ ObjValue = $ setter-> invoke ($ object );

// Now compare
If ($ value = $ objValue ){
Echo "Getter or Setter has modified the data. \ n ";
} Else {
Echo "Getter and Setter does not modify the data. \ n ";
}
}

A little interesting.

Related Article

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.