PHP Reflection Reflectionclass

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.