php反射,看上去显得很高深。
其实,简单说来一句话,就是如果获取类的描述信息,诸如它有多少个属性,每个属性又是啥权限;它又有多少个方法,每个方法的权限又是啥。
本文的目的,就是为了更全面了解类的信息有哪些,以及如何使用反射函数来获取这些信息。
为了方便加深对反射的认识和反射函数的了解,我先捏造了一个简单的学生类,见如下代码:
//php反射函数的简单使用大全
/**
* Class Student
*/
Class Student
{
private $name;
private $age;
private $idno;
const ROLE_NAME = 'student';
/**
* Student constructor.
* @param $name comment:姓名 type:string
* @param $age comment:年龄 type:int
* @param $idno comment:学号 type:string
*/
function __construct($name, $age, $idno){
$this->name = $name;
$this->age = $age;
$this->idno = $idno;
}
public function showInfo(){
echo "idno:".$this->idno."
";
echo "age:".$this->age."
";
echo "name:".$this->name."
";
}
}
从这里开始,下面就是反射函数的具体用法。
注意,阴影部分是代码。
为了方便大家能够对反射函数有直接的了解,紧接着代码后的是执行结果。
目前列举的是个人感觉用得上的,有其它更多的,欢迎大家留言,我会继续补上。
----------- ReflectionObject实例化 -----------$obj = new ReflectionObject(new Student("ZhaiCoder", 30, "20190876")); //实例化反射类
var_dump($obj->getName()); //获取类名string(7) "Student"
var_dump($obj->getDocComment()); //获取类的注释内容string(26) "/**
* Class Student
*/"
总结:获取的注释只包含多行模式的注释,即/* */格式的,而不能能获取单行//的注释内容
var_dump($obj->getFileName()); //获取类所在的文件的绝对目录string(39) "D:\phpStudy\PHPTutorial\WWW\reflect.php"
var_dump($obj->getEndLine()); //获取类最后一行代码所在的行数int(33)
----------- 构造方法/函数 -----------var_dump($obj->getConstructor()); //获取构造函数object(ReflectionMethod)#3 (2) {
["name"]=>
string(11) "__construct"
["class"]=>
string(7) "Student"
}
----------- const常量 -----------var_dump($obj->getConstants()); //获取类中所有常量array(1) {
["ROLE_NAME"]=>
string(7) "student"
}
var_dump($obj->getConstant("ROLE_NAME")); //获取类中指定常量的值string(7) "student"
var_dump($obj->hasConstant("ROLE_NAME")); //判断是否包含常量
var_dump($obj->hasConstant("PREFIX"));bool(true)
bool(false)
----------- 属性 -----------var_dump($obj->getProperties()); //获取类中所有属性array(3) {
[0]=>
object(ReflectionProperty)#3 (2) {
["name"]=>
string(4) "name"
["class"]=>
string(7) "Student"
}
[1]=>
object(ReflectionProperty)#4 (2) {
["name"]=>
string(3) "age"
["class"]=>
string(7) "Student"
}
[2]=>
object(ReflectionProperty)#5 (2) {
["name"]=>
string(4) "idno"
["class"]=>
string(7) "Student"
}
}
var_dump($obj->getProperty("age")); //获取类中指定属性的值object(ReflectionProperty)#5 (2) {
["name"]=>
string(3) "age"
["class"]=>
string(7) "Student"
}
var_dump($obj->hasProperty("age")); //判断类是否包含指定属性
var_dump($obj->hasProperty("sex"));bool(true)
bool(false)
----------- 方法(注意构造函数与大小写) -----------var_dump($obj->getMethods()); //获取类中所有方法array(2) {
[0]=>
object(ReflectionMethod)#5 (2) {
["name"]=>
string(11) "__construct"
["class"]=>
string(7) "Student"
}
[1]=>
object(ReflectionMethod)#4 (2) {
["name"]=>
string(8) "showInfo"
["class"]=>
string(7) "Student"
}
}
var_dump($obj->getMethod("__construct")); //获取类中指定方法object(ReflectionMethod)#4 (2) {
["name"]=>
string(11) "__construct"
["class"]=>
string(7) "Student"
}
var_dump($obj->hasMethod("construct")); //检验名称不完全匹配是否影响bool(false)
var_dump($obj->hasMethod("__construct"));bool(true)
var_dump($obj->hasMethod("showinfo")); //检验大小写是否影响bool(true)总结:判断方法是否存在,方法名必须完全匹配,但忽略大小写
$str = $obj->getMethod("__construct")->getDocComment(); //获取方法的注释内容
var_dump($str);string(181) "/**
* Student constructor.
* @param $name comment:姓名 type:string
* @param $age comment:年龄 type:int
* @param $idno comment:学号 type:string
*/"
注意:获取的注释内容是整条的字符串,并不是按行存储的数组 本文是翟码农个人博客蓝翟红尘里php分类下的有关反射函数使用的文章,转载请注明出处:http://www.zhai14.com/blog/db283f8383839a9d376796578fb6a77e.html
----------- 方法权限相关(public/protected/private等) -----------var_dump($obj->getMethod("__construct")->isConstructor()); //判断是否是构造函数bool(true)
var_dump($obj->getMethod("__construct")->isPrivate()); //判断是否是私有权限bool(false)
var_dump($obj->getMethod("__construct")->isProtected()); //判断是否是保护权限bool(false)
var_dump($obj->getMethod("__construct")->isPublic()); //判断是否是公开权限bool(true)总结:类里方法没有显式声明权限的,则默认为public公开权限
var_dump($obj->getMethod("__construct")->getModifiers()); //获取权限int(134226176)
这里的值有待检查,按我理解,应该是返回256的。
为啥返回256,因为php反射库里ReflectionMethod对象里已经定义好了权限的常量值,具体如下图所示:
var_dump($obj->getMethod("__construct")->getDeclaringClass()); //获取方法所在的类名称object(ReflectionClass)#5 (1) {
["name"]=>
string(7) "Student"
}
下面这个export看结果感觉没啥用,忽略吧。 var_dump($obj->getMethod("__construct")->export($obj->getName(), "__construct"));/**
* Student constructor.
* @param $name comment:姓名 type:string
* @param $age comment:年龄 type:int
* @param $idno comment:学号 type:string
*/
Method [ public method __construct ] {
@@ D:\phpStudy\PHPTutorial\WWW\reflect.php 22 - 26
- Parameters [3] {
Parameter #0 [ $name ]
Parameter #1 [ $age ]
Parameter #2 [ $idno ]
}
}
NULL
本人有一个php交流qq群:296853962,欢迎后端人士加入。 2019年11月01日 22:06文章创建
2019年11月01日 22:58文章发布