Php reflection details and sample code, php details sample code

Source: Internet
Author: User
Tags kohana

Php reflection details and sample code, php details sample code

I recently read the java programming idea and read the type information chapter, which introduces the class information and the concept of reflection. By the way, let's take a look at php reflection. The manual says this: "PHP 5 has a complete reflection API that adds reverse engineering capabilities for classes, interfaces, functions, methods, and extensions. In addition, the reflection API provides methods to retrieve document comments in functions, classes, and methods. "Of course there are some abstractions in the manual! The so-called reverse expression is to get detailed information about classes, methods, attributes, parameters, and so on, including comments! 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 

The ReflectionClass class returns information about a class, such as attributes, methods, namespaces, and interfaces! In the previous example, ReflectionClass: getProperties returns an array of the ReflectionProperty object.

The ReflectionProperty class reports information about the attributes of the class. For example, isDefault isPrivate isProtected isPublic isStatic. The getName method is used to obtain the attribute name!

The above shows how to obtain attributes, and how to obtain attributes such

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());

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

It is not difficult for other objects to look at the manual!

What are the purposes of reflection?

Reflection is a concept of dynamic operation. It can be used to analyze other classes, interfaces, methods, attributes, methods, and extensions. You can also build a mode, such as dynamic proxy. Reflection is often used in some php frameworks, such as kohana and yii. below is the code for implementing mvc in kohana. Reflection is used!

// 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);

The above Code clearly shows the process of this framework! The Router actually processes the url class. The Router can be used to obtain the controller and method! Then execute the method!

The above is to sort out the PHP reflection data, and continue to add relevant information in the future. Thank you for your support for this site!

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.