PHP Reflective Class Reflectionclass

Source: Internet
Author: User
Tags reflection

What is a PHP reflection class, as its name suggests, can be understood as a mapping of a class.

As an example:

Class Fuc {//define a category

static function EC () {

Echo ' I am a class ';

}

}

$class =new reflectionclass (' fuc '); Create a reflection class of fuc this class

Echo $class; Output this reflection class

class [Class A] {@@ f:phpwebmyphptest.php 23-30-constants [0] {}-static properties [0] {}-static methods [0] { }-Properties [0] {}-Methods [1] {method [public method __construct] {@@ f:phpwebmyphptest.php 26-29}}}

$FUC = $class->newinstance (); Equivalent to the instantiation of the Fuc class

$fuc->ec (); Implementation of the FUC method EC

/* Final output: I am a class * *

And some of the more advanced uses

$ec = $class->getmethod (' EC '); Get the EC method in the Fuc class

$FUC = $class->newinstance (); Instantiation of

$ec->invoke ($FUC); Implementation of EC methods

The procedure above is very familiar. is actually similar to the method of calling the object

But here is the reverse, the method in front of the object in the back

Example

try{

If a class with a controller name exists

if (Class_exists ($this->getcontroller ()) {

Using the reflection API to construct a reflection class corresponding to a controller class

$RC = new Reflectionclass ($this->getcontroller ());

If the class implements the IController interface

if ($RC->implementsinterface (' IController ')) {

The class has the method name pointed to by the resolved action string

if ($RC->hasmethod ($this->getaction ())) {

Constructing an instance of a controller class

$controller = $RC->newinstance ();

Gets the method object that the $action parameter of the class points to

$method = $RC->getmethod ($this->getaction ());

How the Reflection class method object is invoked:

$method->invoke ($controller);

} else {

The following are possible exceptions thrown

throw new Exception ("Action");

}

} else {

throw new Exception ("Interface");

}

} else {

throw new Exception ("Controller");

}

}catch (Exception $e)

{

Echo $e;

}

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.