How is the reflection reflection mechanism implemented in PHP?

Source: Internet
Author: User
This article mainly introduces the example of PHP reflection reflection mechanism, this article from the use of reflection to obtain a class of information angle to introduce the reflection reflection mechanism of PHP, the need for friends can refer to the following

PHP5 added a new feature: Reflection. This feature allows programmers to Reverse-engineer class, interface,function,method and extension. With PHP code, you get all the information about an object and you can interact with it.
Suppose 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;        The Public Function setId ($v) {$this->id = $v;        } public Function GetName () {return $this->name;        The Public Function SetName ($v) {$this->name = $v;        } public Function getbiography () {return $this->biography;        The Public Function setbiography ($v) {$this->biography = $v; }}

With Reflectionclass, we can get the following information for the person class:
1. Constant contants
2. Property Names
3. Method Names
4. Static properties
5. Namespace Namespace
Whether the 6.Person class is final or abstract

Just pass the class name "person" to Reflectionclass:

The code is as follows:

$class = new Reflectionclass (' person ');

Get Property (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

If you want to get both the public and private properties, write this: Reflectionproperty::is_public | reflectionproperty::is_protected
Should not feel unfamiliar with it.

The property name can be obtained through $property->getname (), and a comment written to it can be obtained 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

It's kind of weird. Even the annotations can be taken.
Get Method (Methods): Gets all methods to the class through GetMethods (). Returns an array of Reflectionmethod objects. No more demonstrations.
Finally, the method inside the class is called by Reflectionmethod.

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 with $object = new Person (); Get the Getter method and invoke it with the value of our data array $setter = $class->getmethod ("Set". Ucfirst ($k    EY));     $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 "; }}
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.