PHP反射指的是什么

发布时间:2023-02-10 13:45:44 作者:iii
来源:亿速云 阅读:70

这篇“PHP反射指的是什么”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“PHP反射指的是什么”文章吧。

反射,直观理解就是根据到达地找到出发地和来源。 反射指在PHP运行状态中,扩展分析PHP程序,导出或提出关于类、方法、属性、参数等的详细信息,包括注释。这种动态获取信息以及动态调用对象方法的功能称为反射API。

不妨先来看一个demo:

<?php


function p($msg, $var)
{
    echo($msg.":".print_r($var, true)).PHP_EOL.PHP_EOL;
}


class Demo
{
    private $id;

    protected $name;

    public $skills = [];

    public function __construct($id, $name, $skills = [])
    {
        $this->id = $id;
        $this->name = $name;
        $this->skills = $skills;
    }

    public function getName()
    {
        return $this->name;
    }
    public function getSkill()
    {
        p('skill', $this->skills);
    }
}


$ref = new ReflectionClass('Demo');
if ($ref->isInstantiable()) {
    p('检查类是否可实例化isInstantiable', null);
}
$constructor = $ref->getConstructor();
p('获取构造函数getConstructor', $constructor);

$parameters = $constructor->getParameters();
foreach ($parameters as $param) {
    p('获取参数getParameters', $param);
}

if ($ref->hasProperty('name')) {
    $attr = $ref->getProperty('name');
    p('获取属性getProperty', $attr);
}

$attributes = $ref->getProperties();
foreach ($attributes as $row) {
    p('获取属性列表getProperties', $row->getName());
}

if ($ref->hasMethod('getSkill')) {
    $method = $ref->getMethod('getSkill');
    p('获取方法getMethod', $method);
}

$methods = $ref->getMethods();
foreach ($methods as $row) {
    p('获取方法列表getMethods', $row->getName());
}

$instance = $ref->newInstanceArgs([1, 'sai', ['php', 'js']]);
p('newInstanceArgs', $instance);

输出:

➜  php git:(main) php reflect.php 

检查类是否可实例化isInstantiable:

获取构造函数getConstructor:ReflectionMethod Object
(
    [name] => __construct
    [class] => Demo
)


获取参数getParameters:ReflectionParameter Object
(
    [name] => id
)


获取参数getParameters:ReflectionParameter Object
(
    [name] => name
)


获取参数getParameters:ReflectionParameter Object
(
    [name] => skills
)


获取属性getProperty:ReflectionProperty Object
(
    [name] => name
    [class] => Demo
)


获取属性列表getProperties:id

获取属性列表getProperties:name

获取属性列表getProperties:skills

获取方法getMethod:ReflectionMethod Object
(
    [name] => getSkill
    [class] => Demo
)


获取方法列表getMethods:__construct

获取方法列表getMethods:getName

获取方法列表getMethods:getSkill

newInstanceArgs:Demo Object
(
    [id:Demo:private] => 1
    [name:protected] => sai
    [skills] => Array
        (
            [0] => php
            [1] => js
        )

)

demo里面就有使用了ReflectionClass类,当然ReflectionClass类不止于这些方法。

更多方法

ReflectionClass类还有更多方法:

方法说明
ReflectionClass::__construct初始化 ReflectionClass 类
ReflectionClass::export导出一个类
ReflectionClass::getConstant获取定义过的一个常量
ReflectionClass::getConstants获取一组常量
ReflectionClass::getConstructor获取类的构造函数
ReflectionClass::getDefaultProperties获取默认属性
ReflectionClass::getDocComment获取文档注释
ReflectionClass::getEndLine获取最后一行的行数
ReflectionClass::getExtension根据已定义的类获取所在扩展的 ReflectionExtension 对象
ReflectionClass::getExtensionName获取定义的类所在的扩展的名称
ReflectionClass::getFileName获取定义类的文件名
ReflectionClass::getInterfaceNames获取接口(interface)名称
ReflectionClass::getInterfaces获取接口
ReflectionClass::getMethod获取一个类方法的 ReflectionMethod。
ReflectionClass::getMethods获取方法的数组
ReflectionClass::getModifiers获取类的修饰符
ReflectionClass::getName获取类名
ReflectionClass::getNamespaceName获取命名空间的名称
ReflectionClass::getParentClass获取父类
ReflectionClass::getProperties获取一组属性
ReflectionClass::getProperty获取类的一个属性的 ReflectionProperty
ReflectionClass::getReflectionConstantGets a ReflectionClassConstant for a class's constant
ReflectionClass::getReflectionConstantsGets class constants
ReflectionClass::getShortName获取短名
ReflectionClass::getStartLine获取起始行号
ReflectionClass::getStaticProperties获取静态(static)属性
ReflectionClass::getStaticPropertyValue获取静态(static)属性的值
ReflectionClass::getTraitAliases返回 trait 别名的一个数组
ReflectionClass::getTraitNames返回这个类所使用 traits 的名称的数组
ReflectionClass::getTraits返回这个类所使用的 traits 数组
ReflectionClass::hasConstant检查常量是否已经定义
ReflectionClass::hasMethod检查方法是否已定义
ReflectionClass::hasProperty检查属性是否已定义
ReflectionClass::implementsInterface接口的实现
ReflectionClass::inNamespace检查是否位于命名空间中
ReflectionClass::isAbstract检查类是否是抽象类(abstract)
ReflectionClass::isAnonymous检查类是否是匿名类
ReflectionClass::isCloneable返回了一个类是否可复制
ReflectionClass::isFinal检查类是否声明为 final
ReflectionClass::isInstance检查类的实例
ReflectionClass::isInstantiable检查类是否可实例化
ReflectionClass::isInterface检查类是否是一个接口(interface)
ReflectionClass::isInternal检查类是否由扩展或核心在内部定义
ReflectionClass::isIterableCheck whether this class is iterable
ReflectionClass::isIterateable检查是否可迭代(iterateable)
ReflectionClass::isSubclassOf检查是否为一个子类
ReflectionClass::isTrait返回了是否为一个 trait
ReflectionClass::isUserDefined检查是否由用户定义的
ReflectionClass::newInstance从指定的参数创建一个新的类实例
ReflectionClass::newInstanceArgs从给出的参数创建一个新的类实例。
ReflectionClass::newInstanceWithoutConstructor创建一个新的类实例而不调用它的构造函数
ReflectionClass::setStaticPropertyValue设置静态属性的值
ReflectionClass::__toString返回 ReflectionClass 对象字符串的表示形式。

除去强大的ReflectionClass,还有Reflection、ReflectionClassConstant 、ReflectionMethod 、ReflectionFunctionAbstract等等。建议查看手册:

反射的实际应用

反射的优点

反射的缺点

以上就是关于“PHP反射指的是什么”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

推荐阅读:
  1. php怎么查询txt文件是否存在
  2. php怎么去掉html标签样式

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php

上一篇:Composer怎么安装使用

下一篇:jquery如何查询节点内的元素

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》