<?php
class
person{
public
$name
;
public
$gender
;
public
function
say(){
echo
$this
->name,
" \tis "
,
$this
->gender,
"\r\n"
;
}
public
function
set(
$name
,
$value
) {
echo
"Setting $name to $value \r\n"
;
$this
->
$name
=
$value
;
}
public
function
get(
$name
) {
if
(!isset(
$this
->
$name
)){
echo
'未设置'
;
$this
->
$name
=
"正在为你设置默认值"
;
}
return
$this
->
$name
;
}
}
$student
=
new
person();
$student
->name=
'Tom'
;
$student
->gender=
'male'
;
$student
->age=24;
现在,要获取这个student对象的方法和属性列表该怎么做呢?如以下代码所示:
// 获取对象属性列表
$reflect
=
new
ReflectionObject(
$student
);
$props
=
$reflect
->getProperties();
foreach
(
$props
as
$prop
) {
print
$prop
->getName() .
"\n"
;
}
// 获取对象方法列表
$m
=
$reflect
->getMethods();
foreach
(
$m
as
$prop
) {
print
$prop
->getName() .
"\n"
;
}
// 返回对象属性的关联数组
var_dump(get_object_vars(
$student
));
// 类属性
var_dump(get_class_vars(get_class(
$student
)));
// 返回由类的方法名组成的数组
var_dump(get_class_methods(get_class(
$student
)));
// 获取对象属性列表所属的类
echo
get_class(
$student
);
// 反射获取类的原型
$obj
=
new
ReflectionClass(
'person'
);
$className
=
$obj
->getName();
$Methods
=
$Properties
=
array
();
foreach
(
$obj
->getProperties()
as
$v
)
{
$Properties
[
$v
->getName()] =
$v
;
}
foreach
(
$obj
->getMethods()
as
$v
)
{
$Methods
[
$v
->getName()] =
$v
;
}
echo
"class {$className}\n{\n"
;
is_array
(
$Properties
)&&ksort(
$Properties
);
foreach
(
$Properties
as
$k
=>
$v
)
{
echo
"\t"
;
echo
$v
->isPublic() ?
' public'
:
''
,
$v
->isPrivate() ?
' private'
:
''
,
$v
->isProtected() ?
' protected'
:
''
,
$v
->isStatic() ?
' static'
:
''
;
echo
"\t{$k}\n"
;
}
echo
"\n"
;
if
(
is_array
(
$Methods
)) ksort(
$Methods
);
foreach
(
$Methods
as
$k
=>
$v
)
{
echo
"\tfunction {$k}(){}\n"
;
}
echo
"}\n"
;
class
person
{
public
gender
public
name
function
get(){}
function
set(){}
function
say(){}
}
不仅如此,PHP手册中关于反射API更是有几十个,可以说,反射完整地描述了一个类或者对象的原型。反射不仅可以用于类和对象,还可以用于函数、扩展模块、异常等。
反射有什么作用
反射可以用于文档生成。因此可以用它对文件里的类进行扫描,逐个生成描述文档。
既然反射可以探知类的内部结构,那么是不是可以用它做hook实现插件功能呢?或者是做动态代理呢?
例如:
<?php
class
mysql {
function
connect(
$db
) {
echo
"连接到数据库${db[0]}\r\n"
;
}
}
class
sqlproxy {
private
$target
;
function
construct(
$tar
) {
$this
->target[] =
new
$tar
();
}
function
call(
$name
,
$args
) {
foreach
(
$this
->target
as
$obj
) {
$r
=
new
ReflectionClass(
$obj
);
if
(
$method
=
$r
->getMethod(
$name
)) {
if
(
$method
->isPublic() && !
$method
->isAbstract()) {
echo
"方法前拦截记录LOG\r\n"
;
$method
->invoke(
$obj
,
$args
);
echo
"方法后拦截\r\n"
;
}
}
}
}
}
$obj
=
new
sqlproxy(
'mysql'
);
$obj
->connect(
'member'
);
平常开发中,用到反射的地方不多:一个是对对象进行调试,另一个是获取类的信息。在MVC和插件开发中,使用反射很常见,但是反射的消耗也很大,在可以找到替代方案的情况下,就不要滥用。
PHP有Token函数,可以通过这个机制实现一些反射功能。从简单灵活的角度讲,使用已经提供的反射API是可取的。
很多时候,善用反射能保持代码的优雅和简洁,但反射也会破坏类的封装性,因为反射可以使本不应该暴露的方法或属性被强制暴露了出来,这既是优点也是缺点。
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/articles/8681365.html