PHP 反射(Reflection)使用实例

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP 反射(Reflection)使用实例脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

PHP Reflection是用于获取类、扩展、方法函数、对象、参数、属性的详细信息。 ReflectionClass获取类相关信息,如获取属性方法、文档注释等。

<PRe class="brush:PHp;">

class PErson {
/**

  • For the sake of demonstration,we"re setting this private
    */
    private $_allowDynamicAttributes = false;

/* type=Primary_autoincrement /
protected $id = 0;

/* type=vArchar length=255 null /
protected $name;

/* type=text null /
protected $biography;

public function getId()
{
return $this->id;
}
public function setId($v)
{
$this->id = $v;
}
public function getName()
{
return $this->name;
}
public function setName($v)
{
$this->name = $v;
}
public function getBiography()
{
return $this->biography;
}
public function setBiography($v)
{
$this->biography = $v;
}
}

//导出类
ReflectionClass::export('Person');

$r = new ReflectionClass('Person');

//获取所有属性
print_r($r->getProperties());

/**

  • 获取指定属性
  • ReflectionProperty::IS_statIC
  • ReflectionProperty::IS_PUBLIC
  • ReflectionProperty::IS_PROTECTED
  • ReflectionProperty::IS_PRIVATE
    */
    print_r($r->getProperties(ReflectionProperty::IS_PRIVATE));

//获取注释
print_r($r->getProperty('id')->getDocComment());

//获取方法
print_r($r->getmethods());

脚本宝典总结

以上是脚本宝典为你收集整理的PHP 反射(Reflection)使用实例全部内容,希望文章能够帮你解决PHP 反射(Reflection)使用实例所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。