PHP Reflection Class Reflectionclass

Source: Internet
Author: User
Tags vars

PHP Reflection Class Reflectionclass

What is a PHP reflection class, as the name implies, can be understood as a mapping of a class.

As an example:

Class Fuc {//define one of the classes
static function EC () {
Echo' I am a class ';
}
}
$class =new reflectionclass (' fuc '); Set up a reflection class for the Fuc class


Echo $class; Output this reflection class

$FUC = $class->newinstance (); //equivalent to instantiating the FUC class
$fuc->ec (); //implementation of FUC in the method EC
/* Final output: I am a class */
There are some more advanced uses.

$ec = $class->getmethod (' EC '); //Get the EC method in the Fuc class
$FUC = $class->newinstance (); //instantiation
$ec->invoke ($FUC); //Implementation of EC method


The above process is familiar. It's actually similar to the method that invokes the object
But here is the opposite, the method before, the object in the back

Example

[PHP]View Plaincopy
    1. try{
    2. If there is a class for the controller name
    3. if (class_exists ($this->getcontroller ())) {
    4. Construct a reflection class corresponding to a controller class using the Reflection API
    5. $RC = new Reflectionclass ($this->getcontroller ());
    6. If the class implements the IController interface
    7. if ($rc->implementsinterface (' IController ')) {
    8. The class has the method name pointed to by the parsed action string
    9. if ($rc->hasmethod ($this->getaction ())) {
    10. Constructs an instance of a controller class
    11. $controller = $RC->newinstance ();
    12. Gets the method object that the class $action parameter points to
    13. $method = $RC->getmethod ($this->getaction ());
    14. How the Reflection class Method object is called:
    15. $method->invoke ($controller);
    16. } Else {
    17. The following may throw an exception
    18. Throw New Exception ("Action");
    19. }
    20. } Else {
    21. Throw New Exception ("Interface");
    22. }
    23. } Else {
    24. Throw New Exception ("Controller");
    25. }
    26. }catch (Exception $e)
    27. {
    28. echo $e;
    29. }

PHP Reflection Class Reflectionclass

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.