Home  >  Article  >  Backend Development  >  An example introduces PHP's Reflection reflection mechanism, phpreflection_PHP tutorial

An example introduces PHP's Reflection reflection mechanism, phpreflection_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:21:26882browse

An example introduces PHP’s Reflection reflection mechanism, phpreflection

PHP5 adds a new feature: Reflection. This feature allows programmers to reverse-engineer class, interface, function, method and extension. Through PHP code, you can get all the information of an object and interact with it.
Suppose there is a class Person:

Copy code 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 of the Person class:
1.ConstantContants
2. Property Property Names
3. Method Method Names
4. Static Properties
5. Namespace Namespace
6.Whether the Person class is final or abstract

Just pass the class name "Person" to ReflectionClass:

Copy code The code is as follows:

$class = new ReflectionClass('Person');

Get properties (Properties):

Copy code 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 obtain all properties, including private and protected ones. If you only want to get the private attribute, you need to pass an additional parameter:

Copy code The code is as follows:

$private_properties = $class->getProperties(ReflectionProperty::IS_PRIVATE);

Available parameter list:

Copy code The code is as follows:

ReflectionProperty::IS_STATIC
ReflectionProperty::IS_PUBLIC
ReflectionProperty::IS_PROTECTED
ReflectionProperty::IS_PRIVATE

If you want to get both public and private properties, write like this: ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED
It shouldn't feel strange.

The property name can be obtained through $property->getName(), and the comment written to the property can be obtained through getDocComment.

Copy code 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 a bit incredible. You can even get comments.
Get methods: Get all methods of the class through getMethods(). What is returned is an array of ReflectionMethod objects. No more demonstrations.
Finally, the method in the class is called through ReflectionMethod.

Copy code The code is as follows:

$data = array("id" => 1, "name" => "Chris", "biography" => "I am 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 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";
}
}

It’s interesting.

Detailed explanation of Java Reflection (JAVA reflection) mechanism

The score reversal has started again, is it interesting?

What is the reflection mechanism of PHP

It can also be called mapping. To put it bluntly, it can not only clone objects, but also call variables and even methods of objects, which is quite powerful. PHP API5 has an explanation about objects. You can read it if you have a chance. It is similar to the one in

java. Of course, this feature is enough to prove that there is a big difference between php and asp!


http://www.bkjia.com/PHPjc/858748.html

truehttp: //www.bkjia.com/PHPjc/858748.htmlTechArticleAn example introduces PHP’s Reflection reflection mechanism, phpreflection PHP5 adds a new feature: Reflection. This feature allows programmers to reverse-engineer class, interface, function,...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn