PHP Reflection Class Reflectionclass

Source: Internet
Author: User
Tags throw exception

What is a PHP reflection class, as the name implies, can be understood as a mapping of a class.
To give a sample 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

class [ class A] {@@ f:\phpweb\myphp\test.php 23-30-constants [0] {}-static properties [0] {}-static Methods [0] {}-Properties [0] {}-Methods [1] {method [public Method __construct] {@@ f:\phpweb\myph p\test.php 26-29} }}

$FUC = $class->newinstance ();//equivalent to instantiating the FUC class
$fuc->ec ();//method of running FUC EC
/* Final output: I am a class */
Some of the more advanced ways to use

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


The above process is very familiar. In fact, similar to the method of invoking an object
Just here is the reverse, method in front, object in the back

Example

TRY{//assumes the class if the controller name exists (class_exists ($this->getcontroller ())) {//using the Reflection API to construct a controller class corresponding to the Reflection class $RC = new Reflectionclass ($ This->getcontroller ());//Assume that the class implements the IController interface if ($RC->implementsinterface (' IController ')) {// The class has a parsed action string that points to the method name if ($RC->hasmethod ($this->getaction ())) {//constructs an instance of a controller class $controller = $RC Newinstance ();//Gets the method object that the class $action the pointer to $method = $RC->getmethod ($this->getaction ());//Reflection class Method Object Invocation method: $method- >invoke ($controller);} else {//below for possible throw exception throw new Exception ("Action");}} else {throw new Exception ("Interface");}} else {throw new Exception ("Controller");}    } catch (Exception $e)    {        echo $e;    }


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.