The reflection mechanism of PHP

Source: Internet
Author: User
Tags php language vars

the reflection mechanism of PHPCategory: PHP2012-03-15 15:56 13537 People read Comments (3) favorite reports Phppropertiesfunctionmethods Extension API

Directory (?) [+]

Introduced:
PHP5 added a new feature: Reflection. This feature allows Phper 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.
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.
With Reflectionclass, we can get the following information for the person class:
1) Constant contants
2) Property Names
3) method names static
4) Property Static Properties
5) Namespace Namespace
6) Whether the person class is final or abstract
Example
  1. Class Person {
  2. /**  
  3. * For the sake of demonstration, we ' re setting this private
  4. */
  5. private $_allowdynamicattributes = false;
  6. /** type=primary_autoincrement * /
  7. protected $id = 0;
  8. /** type=varchar length=255 null * /
  9. protected $name;
  10. /** type=text null * /
  11. protected $biography;
  12. Public function getId ()
  13. {
  14. return $this->id;
  15. }
  16. Public function setId ($v)
  17. {
  18. $this->id = $v;
  19. }
  20. Public function GetName ()
  21. {
  22. return $this->name;
  23. }
  24. Public function SetName ($v)
  25. {
  26. $this->name = $v;
  27. }
  28. Public function getbiography ()
  29. {
  30. return $this->biography;
  31. }
  32. Public function setbiography ($v)
  33. {
  34. $this->biography = $v;
  35. }
  36. }

Next reflect it, as long as the class name "person" is passed to Reflectionclass, you can:
    1. $class = New Reflectionclass (' person '); Create a reflection class for the person class
    2. $instance = $class->newinstanceargs ($args); The equivalent of instantiating the person class

1) Get Attributes (properties):
    1. $properties = $class->getproperties ();
    2. foreach ($properties as $property) {
    3. echo $property->getname ()."  \ n ";
    4. }
    5. Output:
    6. _allowdynamicattributes
    7. Id
    8. Name
    9. 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:
$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 by $property->getname ().
2) Get comments: You can get a comment written to the property via 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
  10. varchar
  11. Text
3) Gets the method fetch method for the class (methods): Gets all methods to the class by GetMethods ().
4) Method of executing the class:
    1. $instance->getbiography (); //Execute the method in person getbiography
    2. Or:
    3. $ec =$class->getmethod (' getName '); //Get the GetName method in the person class
    4. $ec->invoke ($instance); //Execute GetName method


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The reflection mechanism of PHP

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.