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.