When it comes to reflection reflectionclass everyone probably knows that by encapsulating the various properties of a class for the user to use.
I was very confused at the beginning, since the name of the class know why to use reflection, the online others to give me the example of the reflection is purely for reflection, and then see the Kohana framework of the source of the use of reflection, I understand, a word: Reflection can be used to get some properties that you cannot get from the name of the class.
Code snippet:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$extension = ‘class ‘.$class.‘ extends ‘.$class.‘_Core { }‘; // Start class analysis $core = new ReflectionClass($class.‘_Core‘); if ($core->isAbstract()) { // Make the extension abstract $extension = ‘abstract ‘.$extension; } //var_dump("extension:".$extension); // Transparent class extensions are handled using eval. This is // a disgusting hack, but it gets the job done. eval($extension); |
Here we get the attribute that this class is not an abstract class. And of course there are other properties we can use
Can be obtained in the official PHP reflection document.
The summary is that if you need to instantiate a class, you want to know about other property information about the class in addition to the name of the class, so you should use reflection to get it.