PHP reflection ReflectionClass, phpreflectionclass
Today I encountered such a problem:
ClassA. php
<?phpclass ClassA{public function funcAa(){}public function funcAb(){}public function funcAc(){}}?>
ClassB. php
<?phpinclude './classA.php';class ClassB extends ClassA{public function funcBa(){}public function funcBb(){}public function funcBc(){}public function funcAa(){parent::funcAa();}}$classB = new ClassB;$classFuncB = get_class_methods($classB);echo '<pre>';print_r($classFuncB);?>
When I need to find all the methods in ClassB, the result is as follows:
Array( [0] => funcBa [1] => funcBb [2] => funcBc [3] => funcAa [4] => funcAb [5] => funcAc)
There are 6 methods in total. In fact, I don't want to inherit the methods in ClassA. I just want the ClassB method. What should I do? I slightly changed the following:
$classA = new ClassA;$classB = new ClassB;$classFuncA = get_class_methods($classA);$classFuncB = get_class_methods($classB);echo '<pre>';print_r(array_diff($classFuncB,$classFuncA));
The result is as follows:
Array( [0] => funcBa [1] => funcBb [2] => funcBc)
One method is missing: funcAa. Although funcAa is inherited by ClassB from ClassA, it also has this method, so it is not the result I want.
Solution:
$reflection = new ReflectionClass('ClassB');print_r($reflection->getMethods());
Result:
Array( [0] => ReflectionMethod Object ( [name] => funcBa [class] => ClassB ) [1] => ReflectionMethod Object ( [name] => funcBb [class] => ClassB ) [2] => ReflectionMethod Object ( [name] => funcBc [class] => ClassB ) [3] => ReflectionMethod Object ( [name] => funcAa [class] => ClassB ) [4] => ReflectionMethod Object ( [name] => funcAb [class] => ClassA ) [5] => ReflectionMethod Object ( [name] => funcAc [class] => ClassA ))
We can see that the class value in [4] and [5] Is ClassA, while the other values are ClassB. You can use foreach to achieve the final result:
$ Reflection = new ReflectionClass ('classb '); $ array = ''; foreach ($ reflection-> getMethods () as $ obj) {if ($ obj-> class ==$ reflection-> getName () {// $ reflection-> getName () obtain the class name $ array [] = $ obj-> name ;}} echo '<pre>'; print_r ($ array );
Final result:
Array( [0] => funcBa [1] => funcBb [2] => funcBc [3] => funcAa)
Complete. For more information about ReflectionClass, see the manual.
How does php convert the reflected initialization object to a class object?
Make sure that the class definition file of the User class is introduced. Otherwise, deserialization will not succeed.
If deserialization fails, $ user will not be the instance of the User object, and the getModelName method will not exist.
PHP reflection API problems, urgent