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

PHP reflection ReflectionClass, phpreflectionclass_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:54896browse

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

?>

 

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);
?>

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
)

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));

The results are as follows:

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

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());

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
        )

)

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);

Final result:

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

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