Home > php教程 > php手册 > PHP 反射 ReflectionClass,phpreflectionclass

PHP 反射 ReflectionClass,phpreflectionclass

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 09:22:49
Original
1327 people have browsed it

PHP 反射 ReflectionClass,phpreflectionclass

今天遇到了这样一个问题,如下代码:

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

当我需要找出ClassB里面的所有方法的时候结果如下:

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

一共6个方法,实际上我不想要继承了ClassA里面的方法,我只想要ClassB的方法,怎么办呢?我稍微更改了如下:

$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

结果如下:

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

少了一个方法 funcAa ,虽然funcAa是 ClassB 从 ClassA那里继承过来的,但是同样ClassB也有这个方法,所以不是我想要的结果。

解决方法:

$reflection = new ReflectionClass('ClassB');

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

结果:

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

可以看到 [4]、[5] 里面的class 对应的值是ClassA,而其他对应的值都是ClassB。通过这个可以用foreach来实现最后想要的结果:

$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

最后结果:

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

完成,关于 ReflectionClass 更多的知识请参考手册

php 怎把反射初始化的对象转换成类的对象

你要确定引入了User类的类定义文件,否则反序列化是不会成功的。
反序列化失败,$user就不会是User对象的实例,也就不会存在方法getModelName了。
 

PHP 反射API的问题,急

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template