Home >Backend Development >PHP Tutorial >Obtain object information using PHP's reflection_PHP tutorial

Obtain object information using PHP's reflection_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:33:461107browse

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:

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:

  • Constants
  • Properties Property Names
  • Method Method Names
  • Static Properties
  • Namespace Namespace
  • Whether the Person class is final or abstract

Just pass the class name "Person" to ReflectionClass:

$class = new ReflectionClass('Person');

Get properties (Properties):

$properties = $class->getProperties();
foreach($properties as $property) {
    echo $property->getName()."n";
}
// 输出:
// _allowDynamicAttributes
// id
// name
// biography

By default, ReflectionClass will get all properties, including private and protected ones. If you only want to get the private attribute, you need to pass an additional parameter:

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

Available parameter list:

  • 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.

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, call the method in the class through ReflectionMethod.

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

Interesting.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752417.htmlTechArticlePHP5 adds a new feature: Reflection. This feature allows programmers to reverse-engineer class, interface, function, method and extension. Through PHP code, you can get a certain...
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