PHP Reflection Class Reflectionclass Use Example

Source: Internet
Author: User
Tags reflection

Reflection class is a mapping of a class

namespace News;
Class news{
Public $newsid;
Public Function index () {
......
}
}
Normally we instantiate a class like this: $News = new News\news ();
If we want to instantiate the reflection class of the News class this way: $News = new \reflectionclass (' news\news ');

So what's the difference between a class instantiated by a reflection class and a normal class?

Class instantiated by reflection class we can get the details of this class, we can analyze the class

For example, $News->getproperties () can get all the properties and methods of this class.

Array (size=1)
0 => & Object (Reflectionproperty) [2]
Public ' name ' => string ' NewSID ' (length=6)
Public ' class ' => string ' News ' (length=4)
Getshortname () GetName () Getnamespacename () can obtain the class name, the class name with the namespace, and the name of the namespace news News\news News

One application of Reflectionclass is to get the name of a subclass in the parent class to do different processing for different subclasses:

$reflection = new \reflectionclass ($this);
$class _name = $reflection->getshortname ();
Check key
if (!key_exists ($class _name, $this->configs)) {
return true;
}
Params
$params = $this->configs[$class _name];

    return $this->_handle ($params);
Reflection classes should be used fairly flexibly, and there are quite a few other uses to explore.

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.