来源:自学PHP网 时间:2019-08-01 11:26 作者:小飞侠 阅读:次
[导读] PHP反射学习入门示例...
|
本文实例讲述了PHP反射。分享给大家供大家参考,具体如下:
name = $name;
$this->skills = $skills;
}
public function attack($hero) {
echo "Attack {$hero->name}" . PHP_EOL;
}
public function execute($index) {
echo "Axecute {$index} skill" . PHP_EOL;
}
}
$ref = new ReflectionClass('Hero');
if ($ref->isInstantiable()) {
echo '可以实例化' . PHP_EOL;
}
// 获取类的构造函数
$constructor = $ref->getConstructor();
print_r($constructor); //ReflectionMethod E对象
//获取属性
if ($ref->hasProperty('name')) {
$attr = $ref->getProperty('name');
print_r($attr); //ReflectionProperty 对象
}
// 获取属性列表
$attributes = $ref->getProperties();
foreach ($attributes as $row) {
//row 为 ReflectionProperty 的实例
echo $row->getName() . "\n";
}
// 获取方法
if ($ref->hasMethod('attack')) {
$method = $ref->getMethod('attack');
//$method 为 ReflectionMethod 的实例
print_r($method);
}
// 获取方法列表
$methods = $ref->getMethods();
foreach ($methods as $row) {
//这的row 是 ReflectionMethod 的实例
echo $row->getName() . PHP_EOL;
}
牋 [class] => Hero |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com