Home > Backend Development > PHP Tutorial > PHP reflection ReflectionClass, phpreflectionclass_PHP tutorial

PHP reflection ReflectionClass, phpreflectionclass_PHP tutorial

WBOY
Release: 2016-07-13 10:15:54
Original
914 people have browsed it

PHP Reflection ReflectionClass, phpreflectionclass

I encountered such a problem today, the following code:

classA.php

<?php

class ClassA{
	
	public function funcAa(){
	
	}
	
	public function funcAb(){
	
	}
	
	public function funcAc(){
	
	}
}

?>
Copy after login

 

classB.php

<?php

include './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 class="brush:php;toolbar:false">';

print_r($classFuncB);
?>
Copy after login

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
)
Copy after login

There are 6 methods in total. In fact, I don’t want to inherit the methods in ClassA. I only want the methods in ClassB. What should I do? I changed it slightly as follows:

$classA = new ClassA;

$classB = new ClassB;

$classFuncA = get_class_methods($classA);

$classFuncB = get_class_methods($classB);

echo '<pre class="brush:php;toolbar:false">';

print_r(array_diff($classFuncB,$classFuncA));
Copy after login

The results are as follows:

Array
(
    [0] => funcBa
    [1] => funcBb
    [2] => funcBc
)
Copy after login

There is one missing method funcAa. Although funcAa is inherited by ClassB from ClassA, ClassB also has this method, so it is not the result I want.

Solution:

$reflection = new ReflectionClass('ClassB');

print_r($reflection->getMethods());
Copy after login

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
        )

)
Copy after login

You can see that the corresponding value of the class in [4] and [5] is ClassA, while the other corresponding values ​​are ClassB. Through 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()  获取类名
		
		$array[] = $obj->name;
	
	}

}

echo '<pre class="brush:php;toolbar:false">';

print_r($array);
Copy after login

Final result:

Array
(
    [0] => funcBa
    [1] => funcBb
    [2] => funcBc
    [3] => funcAa
)
Copy after login

Completed, for more knowledge about ReflectionClass, please refer to the manual

php How to convert reflection initialized objects into class objects

You must make sure to introduce the class definition file of the User class, otherwise the deserialization will not succeed.
Deserialization fails, $user will not be an instance of the User object, and the method getModelName will not exist.

PHP reflection API problem, urgent

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/900990.htmlTechArticlePHP ReflectionReflectionClass, phpreflectionclass I encountered such a problem today, the following code: classA.php ?phpclass ClassA{public function funcAa(){}public function funcAb(){}...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template