PHP Reflection reflectionclass,phpreflectionclass_php Tutorial

Source: Internet
Author: User

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 () {} ...

  • 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.