Hyperf2.0 新功能早知道 | PHP 技术论坛


本站和网页 https://learnku.com/articles/45031 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

Hyperf2.0 新功能早知道 | PHP 技术论坛
PHP
话题列表
社区 Wiki
优质外文
招聘求职
PHP 实战教程
社区文档
登录
注册
PHP
首页
Laravel
Go
PHP
Vue.js
Python
Java
MySQL
Rust
LK
Elasticsearch
F2E 前端
Server
程序员
Database
DevTools
Computer Science
手机开发
AdonisJS
社区
Wiki
文档
社区文档首页
《PHP 内核与原生扩展开发》
《Composer 中文文档》
《Elasticsearch-PHP 中文文档》
《PHP PSR 标准规范》
《PHP 设计模式全集》
登录
注册
微信登录
Hyperf2.0 新功能早知道
李铭昕 的个人博客
307
创建于 2年前
在使用 Hyperf1.1 的小伙伴们,通常都会碰到这么一个问题,那就是协程上下文数据拷贝的问题。
比如我实现了一个 Listener,在监听 SQL 的同时,会把当前请求的路由数据记录下来。当出现慢查的时候,就可以精确定位到是哪个路由。
但当你以以下方式执行 SQL 时,可能就会出现这个错误 TypeError:Return value of Hyperf\HttpServer\Request::getRequest() must implement interface Psr\Http\Message\ServerRequestInterface, null returned
<?php
go(function(){
Db::select("SELECT * FROM users;");
})
当然,你可以自己实现 go 方法,然后在 composer 加载之前提前加载这个文件。例如
// 包含 go 方法的实现
require BASE_PATH . '/config/bootstrap.php';
require BASE_PATH . '/vendor/autoload.php';
但框架中直接使用 Coroutine::create 来创建协程就没有办法了,比如 Parallel 和 Concurrent 等等
ClassMap 功能
而在 Hyperf2.0 版本中,框架实现了基于 composer class map 的替换功能,你可以自己实现 Hyperf\Utils\Coroutine,而框架会自动帮你替换掉原来的 Hyperf\Utils\Coroutine。
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Utils;
use App\Kernel\Context\Coroutine as BCoroutine;
use Swoole\Coroutine as SwooleCoroutine;
/**
* @method static void defer(callable $callable)
*/
class Coroutine
public static function __callStatic($name, $arguments)
if (! method_exists(SwooleCoroutine::class, $name)) {
throw new \BadMethodCallException(sprintf('Call to undefined method %s.', $name));
return SwooleCoroutine::$name(...$arguments);
/**
* Returns the current coroutine ID.
* Returns -1 when running in non-coroutine context.
*/
public static function id(): int
return SwooleCoroutine::getCid();
/**
* Returns the parent coroutine ID.
* Returns -1 when running in the top level coroutine.
* Returns null when running in non-coroutine context.
* @see https://github.com/swoole/swoole-src/pull/2669/files#diff-3bdf726b0ac53be7e274b60d59e6ec80R940
*/
public static function parentId(): ?int
$cid = SwooleCoroutine::getPcid();
if ($cid === false) {
return null;
return $cid;
/**
* @return int Returns the coroutine ID of the coroutine just created.
* Returns -1 when coroutine create failed.
*/
public static function create(callable $callable): int
return di()->get(BCoroutine::class)->create($callable);
public static function inCoroutine(): bool
return Coroutine::id() > 0;
App\Kernel\Context\Coroutine 是实现了协程上下文拷贝功能的类。
测试
让我们写一段代码,进行测试。
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Controller;
class IndexController extends Controller
public function index()
$user = $this->request->input('user', 'Hyperf');
$method = $this->request->getMethod();
go(function () {
var_dump($this->request->input('user'));
});
return $this->response->success([
'user' => $user,
'method' => $method,
'message' => 'Hello Hyperf.',
]);
当我们不使用 ClassMap 功能时,调用接口,会抛出以下错误。
[WARNING] TypeError:Return value of Hyperf\HttpServer\Request::getRequest() must implement interface Psr\Http\Message\ServerRequestInterface, null returned(0) in /Users/limx/Applications/GitHub/hyperf/biz-skeleton/vendor/hyperf/http-server/src/Request.php:620
Stack trace:
#0 /Users/limx/Applications/GitHub/hyperf/biz-skeleton/vendor/hyperf/http-server/src/Request.php(579): Hyperf\HttpServer\Request->getRequest()
#1 /Users/limx/Applications/GitHub/hyperf/biz-skeleton/vendor/hyperf/utils/src/Functions.php(268): Hyperf\HttpServer\Request->Hyperf\HttpServer\{closure}()
#2 /Users/limx/Applications/GitHub/hyperf/biz-skeleton/vendor/hyperf/http-server/src/Request.php(593): call(Object(Closure))
#3 /Users/limx/Applications/GitHub/hyperf/biz-skeleton/vendor/hyperf/http-server/src/Request.php(587): Hyperf\HttpServer\Request->storeParsedData(Object(Closure))
#4 /Users/limx/Applications/GitHub/hyperf/biz-skeleton/vendor/hyperf/http-server/src/Request.php(97): Hyperf\HttpServer\Request->getInputData()
#5 /Users/limx/Applications/GitHub/hyperf/biz-skeleton/app/Controller/IndexController.php(21): Hyperf\HttpServer\Request->input('user')
#6 /Users/limx/Applications/GitHub/hyperf/biz-skeleton/vendor/hyperf/utils/src/Functions.php(268): App\Controller\IndexController->App\Controller\{closure}()
#7 /Users/limx/Applications/GitHub/hyperf/biz-skeleton/vendor/hyperf/utils/src/Coroutine.php(67): call(Object(Closure))
#8 {main}
当我们配置了 ClassMap 后,结果就正常了。
$ curl http://127.0.0.1:9501/\?user\=Hyperf
{"code":0,"data":{"user":"Hyperf","method":"GET","message":"Hello Hyperf."}}
string(6) "Hyperf"
可见 App\Kernel\Context\Coroutine 已被正确替换。
大家可以考虑一下,哪些场景,通过这个办法可以轻松的解决呢?
写在最后
Hyperf 是基于 Swoole 4.4+ 实现的高性能、高灵活性的 PHP 协程框架,内置协程服务器及大量常用的组件,性能较传统基于 PHP-FPM 的框架有质的提升,提供超高性能的同时,也保持着极其灵活的可扩展性,标准组件均基于 PSR 标准 实现,基于强大的依赖注入设计,保证了绝大部分组件或类都是 可替换 与 可复用 的。
框架组件库除了常见的协程版的 MySQL 客户端、Redis 客户端,还为您准备了协程版的 Eloquent ORM、WebSocket 服务端及客户端、JSON RPC 服务端及客户端、GRPC 服务端及客户端、Zipkin/Jaeger (OpenTracing) 客户端、Guzzle HTTP 客户端、Elasticsearch 客户端、Consul 客户端、ETCD 客户端、AMQP 组件、Apollo 配置中心、阿里云 ACM 应用配置管理、ETCD 配置中心、基于令牌桶算法的限流器、通用连接池、熔断器、Swagger 文档生成、Swoole Tracker、Blade 和 Smarty 视图引擎、Snowflake 全局ID生成器 等组件,省去了自己实现对应协程版本的麻烦。
Hyperf 还提供了 基于 PSR-11 的依赖注入容器、注解、AOP 面向切面编程、基于 PSR-15 的中间件、自定义进程、基于 PSR-14 的事件管理器、Redis/RabbitMQ 消息队列、自动模型缓存、基于 PSR-16 的缓存、Crontab 秒级定时任务、Translation 国际化、Validation 验证器 等非常便捷的功能,满足丰富的技术场景和业务场景,开箱即用。%
本作品采用《CC 协议》,转载必须注明作者和本文链接
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
举报
李铭昕
483 声望
作者 @ Hyperf
暂无个人描述~
1 人点赞
推荐文章:
更多推荐...
博客
PHP面试经常被问到的知识点汇总,对你非常有用
38
1年前
博客
[hyperf]hyperf-ext/auth和hyperf-ext/jwt完成jwt认证与自动刷新token
23
19
2年前
翻译
PHP 7.4 新功能更新列表
13
3年前
博客
令人期待的 PHP7.4
16
3年前
讨论数量: 4
排序:
时间
投票
_杭城浪子
课程读者
390 声望
PHP & java @ nc
喜大普奔
2年前
评论
评论
举报
Artist0618
课程读者
24 声望
Hyperf作者,爱了爱了
2年前
评论
评论
举报
huangxu
见习助教
60 声望
虽然不懂,但感觉很厉害的样子
2年前
评论
评论
举报
迷途的羔羊
0 声望
哪个文件做ClassMap替换Hyperf\Utils\Coroutine,如果是App\Kernel\Context\Coroutine替换Hyperf\Utils\Coroutine,此方法会报错
4个月前
评论
评论
举报
讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
<a href="javascript:;" class="mr-2 ui popover text-mute" data-html="黏贴或拖拽图片至输入框内皆可上传图片">
<a href="javascript:;" class="mr-2 ui popover text-mute hide-on-mobile" data-html="支持除了 H1~H6 以外的GitHub 兼容 Markdown">
支持 MD
帮助
关注本文
评论
李铭昕
作者 @ Hyperf
文章
33
粉丝
175
喜欢
165
收藏
94
排名:180
访问:5.2 万
关注
私信
所有博文
阅读模式
文章归档
1 篇
2022 年 7 月
1 篇
2022 年 6 月
1 篇
2021 年 12 月
1 篇
2021 年 11 月
1 篇
2021 年 9 月
3 篇
2021 年 2 月
2 篇
2021 年 1 月
3 篇
2020 年 12 月
1 篇
2020 年 10 月
1 篇
2020 年 8 月
1 篇
2020 年 5 月
2 篇
2020 年 4 月
2 篇
2020 年 1 月
5 篇
2019 年 10 月
1 篇
2019 年 9 月
2 篇
2019 年 8 月
1 篇
2019 年 6 月
1 篇
2019 年 3 月
1 篇
2018 年 11 月
2 篇
2018 年 7 月
最新文章
最受欢迎
5个月前
使用 Micro 打包 PHP 应用
6个月前
GitHub 成就系统出来了,大家快去看看
1年前
今天注定Javaer无眠
1年前
聊聊现在PHP工程师的现状
1年前
[招聘]上海长宁区 PHP中高级开发工程师
22
Aop 设计 - 使用 PHP-parser 重写 PHP 类
14
基于注解的 PHP 枚举类实现
12
如何基于 Channel 实现多路复用
11
创建适合自己的骨架包
Laravel+Swoole+PHP-ml 实现根据经纬度返回对应城市
博客标签
redis
php
laravel
jwt
task
docker
swoole
gitlab
Skeleton
demo
vue
AMQP
Tesseract
easyswoole
swoft
swarm
机器学习
hyperf
11
CI/CD
Uniapp
社区赞助商
成为赞助商
社区赞助商
成为赞助商
关于 LearnKu
LearnKu 是终身编程者的修道场
做最专业、严肃的技术论坛
LearnKu 诞生的故事
资源推荐
《社区使用指南》
《文档撰写指南》
《LearnKu 社区规范》
《提问的智慧》
服务提供商
其他信息
成为版主
所有测验
联系站长(反馈建议)
粤ICP备18099781号-6
粤公网安备 44030502004330号
违法和不良信息举报
由 Summer 设计和编码 ❤
请登录
提交
忘记密码?
or
注册
第三方账号登录
微信登录
GitHub 登录
内容举报
匿名举报,为防止滥用,仅管理员可见举报者。
我要举报该,理由是:
垃圾广告:恶意灌水、广告、推广等内容
无意义内容:测试、灌水、文不对题、消极内容、文章品质太差等
违规内容:色情、暴利、血腥、敏感信息等
不友善内容:人身攻击、挑衅辱骂、恶意行为
科学上网:翻墙、VPN、Shadowsocks,政策风险,会被关站!
不懂提问:提问太随意,需要再做一遍《提问的智慧》测验
随意提问:提问没有发布在社区问答分类下
排版混乱:没有合理使用 Markdown 编写文章,未使用代码高亮
内容结构混乱:逻辑不清晰,内容混乱,难以阅读
标题随意:标题党、标题不释义
尊重版权:分享付费课程、破解软件(付费),侵犯作者劳动成果
其他理由:请补充说明
举报
取消