PHP Reflection Learning Getting Started Sample

  • 2021-12-12 03:45:00
  • OfStack

PHP reflection is described as an example in this paper. Share it for your reference, as follows:

Today, I began to learn the reflection of php. Many people may not have heard of the concept of reflection. Simply put, reflection gives you the ability to analyze classes and functions.

Some students may ask me what is the use of profiling classes and why I want to learn reflection. I can only say that not learning reflection will not have any impact on your business implementation, but if you want to write programs with elegant structure and high maintainability and extensibility, learning reflection is essential.

PHP has built-in 1 set of reflection classes to realize class reflection, which are commonly used as follows:

ReflectionClass parsing class Information about the properties of the ReflectionProperty class Information about ReflectionMethod class methods ReflectionParameter retrieves information about function or method parameters

If you want to read everything, turn over the manual.

Today, let's take a brief look at the reflection of php through a demonstration code.


<?php
class Hero {
  protected $name;
  protected $skills = [];
  public function __construct($name, $skills = []) {
    $this->name = $name;
    $this->skills = $skills;
  }
  public function attack($hero) {
    echo "Attack {$hero->name}" . PHP_EOL;
  }
  public function execute($index) {
    echo "Axecute {$index} skill" . PHP_EOL;
  }
}
$ref = new ReflectionClass('Hero');
if ($ref->isInstantiable()) {
  echo ' You can instantiate ' . PHP_EOL;
}
//  Gets the constructor of the class 
$constructor = $ref->getConstructor();
print_r($constructor); //ReflectionMethod E Object 
// Get properties 
if ($ref->hasProperty('name')) {
  $attr = $ref->getProperty('name');
  print_r($attr); //ReflectionProperty  Object 
}
//  Get a list of attributes 
$attributes = $ref->getProperties();
foreach ($attributes as $row) {
  //row  For  ReflectionProperty  Example of 
  echo $row->getName() . "\n";
}
//  Acquisition method 
if ($ref->hasMethod('attack')) {
  $method = $ref->getMethod('attack');
  //$method  For  ReflectionMethod  Example of 
  print_r($method);
}
//  Get a list of methods 
$methods = $ref->getMethods();
foreach ($methods as $row) {
  // Here row  Yes  ReflectionMethod  Example of 
  echo $row->getName() . PHP_EOL;
}

Run results:

You can instantiate
ReflectionMethod Object
(
[name] = > __construct
[class] = > Hero
)
ReflectionProperty Object
(
[name] = > name
[class] = > Hero
)
name
skills
ReflectionMethod Object
(
[name] = > attack
[class] = > Hero
)
__construct
attack
execute

To learn new things, first of all, you must have a sense of familiarity, or hand feeling. If you have a hand feeling, you will not be afraid of it. First, copy the above code and run it for 1 time. You should have a preliminary impression of php reflection.

In the next article, do another small example to see what magical things can be done with hair.

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: