【PHP高级特性】之反射

发布时间:2019-08-07 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了【PHP高级特性】之反射脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

PHP5 开始提供了完整的反射API。有反射类(ReflectionClass)和反射函数(ReflectionFunction)等,功能大同小异,这里主要以ReflectionClass为列说明。

什么是反射
他是指PHP在运行状态中,动态的获取类、方法、属性、参数、注释等信息和动态调用对象的方法的功能。

有什么用
可以帮助我们构建复杂的,可扩的运用。比如自动加载插件,自动生成文档等

代码示例
该示例为一个通用API入口

HttpApi.php

namespace twinkleservicehttp;

class HttpApi
{
    PRivate $class;

    public function __construct($class)
    {
        $this->class = $class;
    }

    public function parseRequest($method,$params = [])
    {
        $class = new ReflectionClass($this->class);
        $instance = $class->newInstanceArgs($params);
        $method = $class->getMethod($method);
        $args = [];
        foreach ($method->getParameters() as $param) {
            $name = $param->getName();
            if (isset($params[$name])) {
                $args[$name] = $params[$name];
            } else {
                try {
                    $args[$name] = $param->getDefaultValue();
                } catch (Exception $e) {
                    throw new RequestException(
                        '请求参数不合未能',
                        500
                    );
                }
            }
        }

        return [$instance,$method,$args];
    }
}

NotFoundService.php

namespace appservices;

use appbaseService;

class NotFoundService extends Service
{
    public function error()
    {
        return $this->format(['status' => 1, 'msg' => '请求不合法,请确认service和method是否存在']);
    }
}

使用范例

$params = $_REQUEST;
$serviceName= isset($params['service']) ? $params['service'] : 'NotFound';
$methodName= isset($params['method']) ? $params['method'] : 'error';
$class = '\app\services\' . Str::ucWords($serviceName) . 'Service';
list($instance, $method, $args) = (new HttpApi($class))->parseRequest($methodName, $params);
echo json_encode(($method->invokeArgs($instance, $args)));

脚本宝典总结

以上是脚本宝典为你收集整理的【PHP高级特性】之反射全部内容,希望文章能够帮你解决【PHP高级特性】之反射所遇到的问题。

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

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