PHP reflection ReflectionClass, phpreflectionclass

Source: Internet
Author: User

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

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.