Introduction to the reflection reflection mechanism of PHP

Source: Internet
Author: User
This article mainly introduces the introduction of the reflection reflection mechanism of PHP, has a certain reference value, now share to everyone, the need for friends can refer to

PHP5 added a new feature: Reflection. This feature allows programmers to

reverse-engineer[Reverse engineering] class, Interface,function,method and extension[Extension Library support].

With PHP code, you get all the information about an object and you can interact with it.

For example, assume the following person class:

 1 class Person {2/** 3 * for the SA  Ke of demonstration, we "re setting this private 4 */5 Private $_allowdynamicattributes = false; 6 7/** 8 * Type=primary_autoincrement 9 * * protected $id = 0; /** * Type=varchar length=255 NULL */protected $name; /** * Type=text null19 */protected $biography; () Public Function getId () {return $this->id;), Public function setId ($v) {$t His->id = $v; -Public Function GetName () {$this->name;}-Public Function SetName ($v) {$this->name = $v; Getbiography () "Public Function" () {return $this->biography; *->biography Public Function setbiography ($v) {PNs $this = $v;

With Reflectionclass, we can get the following information for the person class:

    • Constant contants

    • Property Names

    • Method Names

    • Static Properties Statics Property

    • Namespace Namespace

    • Whether the person class is final or abstract

Just pass the class name "person" to Reflectionclass:

1 $class = new Reflectionclass (' person ');

* Get Attributes (properties):

1 $properties = $class->getproperties (); 2 foreach ($properties as $property) {3     echo $property->getname (). " \ n "; 4}5//output: 6//_ALLOWDYNAMICATTRIBUTES7//ID8//NAME9//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:

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

List of available parameters:

    • Reflectionproperty::is_static

    • Reflectionproperty::is_public

    • reflectionproperty::is_protected

    • Reflectionproperty::is_private

If you want to get both the public and private properties, write this: Reflectionproperty::is_public | reflectionproperty::is_protected

The property name can be obtained through $property->getname (), and a comment written to it can be obtained through getdoccomment.

1 foreach ($properties as $property) {2     if ($property->isprotected ()) {  3         $docblock = $property Getdoccomment ();  4         Preg_match ('/type\= ([a-z_]*)/', $property->getdoccomment (), $matches);  5         Echo $matches [1]. " \ n ";  6     }  7}  8//Output:  9//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.

$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"; }}

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.