PHP reflection and sample code _php instance

Source: Internet
Author: User
Tags reflection kohana

Recently looking at Java programming ideas, see the chapter of type information, the information of class and the concept of reflection. By the way warm up PHP reflection. The manual says: "PHP 5 has a complete reflection API that adds the ability to reverse engineer classes, interfaces, functions, methods, and extensions." In addition, the reflection API provides a way to remove documentation comments from functions, classes, and methods. "Of course the handbook says something abstract!" The so-called reverse white is to get information about classes, methods, properties, parameters, etc., including comments! The text is always so boring, for instance

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 


Reflectionclass This class returns the relevant information about a class, such as attributes, methods, namespaces, implementation of those interfaces, and so on! In the previous example Reflectionclass:: GetProperties Returns an array of Reflectionproperty objects.

The Reflectionproperty class reports information about the properties of the class. For example IsDefault isprivate isprotected ispublic isstatic, Method GetName is to get the name of the property!

The above is to get the attribute, as well as to get the class method

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, Reflectionmethod class reports information about a method, such as IsAbstract IsPrivate isprotected ispublic isstatic Isconstructor, there is an important way Invoke,invokeargs is the implementation of the Method!

Other objects can look at manuals, not very hard!

What is the use of reflection?

Reflection is a dynamic concept that can be used synthetically to help us analyze other classes, interfaces, methods, attributes, methods, and extensions. You can also build schemas, such as dynamic proxies. It's also very common to use reflection in some PHP frameworks, such as Kohana,yii, which is the code for Kohana to implement MVC, which is used for 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);

The above code can clearly see the process of this framework! Through router in fact on the processing of the URL class, through the router can get which controller, which method! And then execute the method!

The above is to the PHP reflection of the data collation, follow-up continue to supplement the relevant information, thank you for your support of 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.