Today, we have a problem with the following code:
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> ';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 ' <pre> ';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 ' <pre> ';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
PHP Reflection Reflectionclass