Detailed explanation and sample code of reflection in php

高洛峰
Release: 2023-03-04 07:48:01
Original
1279 people have browsed it

I was reading Java Programming Thoughts recently and saw the chapter on type information, which talked about class information and the concept of reflection. By the way, let’s review the reflection tools of php. Here's what the manual says: "PHP 5 has a complete reflection API, adding the ability to reverse engineer classes, interfaces, functions, methods, and extensions. In addition, the reflection API provides methods to take out reflections in functions, classes, and methods." Documentation comment. "Of course the manual is a bit abstract! The so-called reverse engineering is to get detailed information about classes, methods, attributes, parameters, etc., including comments! The text is always so boring, for example

class Foo {
  public  $foo = 1;
  protected $bar = 2;
  private  $baz = 3;
   
  /**
   * Enter description here ...
   */
  public function myMethod()
  {
    echo 'hello 2b';
  }
}
 
$ref = new ReflectionClass('Foo');
$props = $ref->getProperties();
foreach ($props as $value) {
  echo $value->getName()."\n";
}
 
//output
//foo 
//bar
//baz
Copy after login

ReflectionClass This class returns relevant information about a certain class, such as attributes , methods, namespaces, implementing those interfaces, etc.! In the previous example, ReflectionClass::getProperties returned an array of ReflectionProperty objects. The

ReflectionProperty class reports information about the properties of a class. For example, isDefault isPrivate isProtected isPublic isStatic, etc., the method getName is to get the name of the attribute!

The above is for obtaining attributes, and there are also methods for obtaining class methods, such as

class Foo {
  public  $foo = 1;
  protected $bar = 2;
  private  $baz = 3;
   
  /**
   * Enter description here ...
   */
  public function myMethod()
  {
    echo 'hello 2b';
  }
}
 
$ref = new ReflectionClass('Foo');
$method = $ref->getMethod('myMethod');
$method->invoke($ref->newInstance());
Copy after login

ReflectionClass::getMethod is a ReflectionMethod type. The ReflectionMethod class reports information about a method, such as isAbstract isPrivate isProtected isPublic isStatic isConstructor, and there is The important methods Invoke and InvokeArgs are the execution methods!

For other objects, you can read the manual, it is not difficult!

What are the uses of reflection?

Reflection is a dynamically running concept. Used together, they can be used to help us analyze other classes, interfaces, methods, properties, methods and extensions. Patterns can also be built, such as dynamic proxies. It is also very common to use reflection in some PHP frameworks, such as kohana and yii. The following is the code of kohana to implement mvc, which uses reflection!

// Start validation of the controller
$class = new ReflectionClass(ucfirst(Router::$controller).'_Controller');
// Create a new controller instance
$controller = $class->newInstance();
// Load the controller method
$method = $class->getMethod(Router::$method);
// Execute the controller method
$method->invokeArgs($controller, $arguments);
Copy after login

The above code can clearly see the process of this framework! Through Router, you actually process the url class. Through Router, you can get which controller and which method! Then execute the method again!

The above is the collection of information on PHP reflection. We will continue to add relevant information in the future. Thank you for your support of this site!

For more PHP reflection details and sample code related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!