当前位置: 首页 > news >正文

php闭包应用

laravel 路由 bingTo 把路由URL映射到匿名回调函数上,框架会把匿名回调函数绑定到应用对象上,这样在匿名函数中就可以使用$this关键字引用重要的应用对象。Illuminate\Support\Traits\Macroable的__call方法。
在这里插入图片描述
自己写一个简单的demo:

<?php
<?phpnamespace Hjj\DesignPatterns\Tests;use PHPUnit\Framework\TestCase;
class App {protected $routes = [];protected $responseStatus = '200 OK';protected $responseContentType = 'text/html';protected $responseBody = 'Laravel学院';public function addRoute($routePath, $routeCallback) {$this->routes[$routePath] = $routeCallback->bindTo($this, __CLASS__);}public function dispatch($currentPath) {foreach ($this->routes as $routePath => $callback) {if( $routePath === $currentPath) {$callback();}}
//        header('HTTP/1.1 ' . $this->responseStatus);
//        header('Content-Type: ' . $this->responseContentType);
//        header('Content-Length: ' . mb_strlen($this->responseBody));return $this->responseBody;}}
class Closure extends TestCase
{public function testClosure () {$app = new App();$app->addRoute('user/nonfu', function(){$this->responseContentType = 'application/json;charset=utf8';$this->responseBody = '{"name":"LaravelAcademy"}';});$result = $app->dispatch('user/nonfu');$this->assertSame('{"name":"LaravelAcademy"}', $result);}
}
http://www.lryc.cn/news/323914.html

相关文章:

  • 基于python+vue的OA公文发文管理系统flask-django-php-nodejs
  • 脉冲变压器电感的工艺结构原理及选型参数总结
  • java中Arrays介绍及常用方法
  • CTF题型 Http请求走私总结Burp靶场例题
  • Nginx 的安装、启动和关闭
  • python 操作excel(openpyxl.load_workbook)、excel操作封装
  • MySQL系统参数配置实战:生产环境优化
  • 判断列表中每一个元素的个数
  • 目标检测——PP-YOLOE算法解读
  • 每日一题 --- 螺旋矩阵 II[力扣][Go]
  • C语言自定义类型结构体
  • 【SpringBoot框架篇】37.使用gRPC实现远程服务调用
  • 投资的三个匹配
  • [Netty实践] 请求响应同步实现
  • Java进阶—哈希冲突的解决
  • css的border详解
  • 如何保障消息一定能发送到RabbitMQ?
  • 【web前端】CSS语法
  • JS+CSS3点击粒子烟花动画js特效
  • docker镜像复制与常见命令
  • 如何在linux环境上部署单机ES(以8.12.2版本为例)
  • 如何利用人工智能技术实现企业营销效率提升10倍(下)
  • 【PHP + 代码审计】数组函数
  • Keepalive与idle监测及性能优化
  • DS-红黑树(RBTree)
  • ubuntu 如何使用阿里云盘
  • sqlite3 交叉编译
  • 【AI生成文章】flutter ChangeNotifierProvider 实用场景举例
  • 【0274】从shared init file或local init file加载relation cache(2 - 1)
  • 蓝桥杯-02-2023蓝桥杯c/c++省赛B组题目