PHP Reflection Reflectionclass,phpreflectionclass
Today, we have a problem with the following code:
classa.php
classb.php
';p rint_r ($classFuncB);? >
When I need to find out all the methods in CLASSB, the result is as follows:
Array ( [0] = FUNCBA [1] = FUNCBB [2] = FUNCBC [3] = = FUNCAA [4] = = Funcab [5] = > FUNCAC)
Altogether 6 methods, actually I do not want to inherit the ClassA inside the method, I only want ClassB method, how to do? I changed a little bit as follows:
$classA = new ClassA; $classB = new ClassB; $classFuncA = Get_class_methods ($classA); $classFuncB = Get_class_methods ($ ClassB); Echo '';p Rint_r (Array_diff ($classFuncB, $classFuncA));
The results are as follows:
Array ( [0] = FUNCBA [1] = FUNCBB [2] = FUNCBC)
One less method FUNCAA, although FUNCAA is ClassB inherit from ClassA, but same ClassB also have this method, so not I want the result.
Workaround:
$reflection = new Reflectionclass (' ClassB ');p rint_r ($reflection->getmethods ());
Results:
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 ))
You can see that the value of class corresponding to [4], [5] is ClassA, and the other corresponding values are CLASSB. With this you can use foreach to achieve the final desired result:
$reflection = new Reflectionclass (' ClassB '); $array = "; foreach ($reflection->getmethods () as $obj) {if ($obj, class = = $reflection->getname ()) {//$reflection->getname () Gets the class name $array[] = $obj->name;}} Echo '';p rint_r ($array);
Final Result:
Array ( [0] = FUNCBA [1] = FUNCBB [2] = = FUNCBC [3] = = FUNCAA)
Complete, for more information about Reflectionclass please refer to the manual
How PHP converts objects that reflect initialization to objects of a class
You have to be sure that the class definition file of the user class is introduced, otherwise deserialization will not succeed.
If the deserialization fails, the $user will not be an instance of the user object, and there will be no method getmodelname.
PHP Reflection API problem, urgent
http://www.bkjia.com/PHPjc/900990.html www.bkjia.com true http://www.bkjia.com/PHPjc/900990.html techarticle PHP Reflection Reflectionclass,phpreflectionclass today encountered a problem with the following code: classa.php? phpclass classa{public function Funcaa () {} Public Function Funcab () {} ...