我这是hyperf框架 , php8.0
这里通过rabbitmq投递任务 , 在消费者中调用分发方法进行不同操作(异步 , 节省请求时间)
通过变量的值获取是否有同名的常量 , 并获取这个常量的值
真的是难到我了 , 下面是代码 , php函数自己研究一下吧
<?php
/**
* 积分服务
*
* @authors Msy
* @Created-Time: 2023/1/31 20:27
*/
namespace App\Services;
use App\Amqp\Producer\PointAddProducer;
use Hyperf\Amqp\Producer;
use Hyperf\Utils\ApplicationContext;
class PointsService
{
const REAL_NAME = 1;// 实名
const SHARE_ORDER_DAILY = 2;// 每日分享订单
const DAILY_PUBLISH_ORDER = 3;// 每日发单
const DAILY_RECERVING_ORDER = 4;// 每日接单
const INCREASE_PUBLISH_ORDER = 5;// 累计发单
const INCREASE_RECERVING_ORDER = 6;//累计接单
/***
* 投递消息
*
* @authors: Msy
* @Created-Time: 2023/2/1 9:10
* @param array $params
* @return void
*/
public function delivery(array $params)
{
$message = new PointAddProducer($params);
$producer = ApplicationContext::getContainer()->get(Producer::class);
return $producer->produce($message);
}
# 分发处理
public function entry(array $params)
{
if (!$params['type']) {
return true;
}
//如果类常量不存在就代表这是无效的 , 直接返回就完了
if (!defined('self::' . $params['type'])) {
return true;
}
// match (self::$params['type']) { // 这里是错误的 , 把$params当成了类静态变量 , 而且不能写成 self::{$params['type']} , 因为以后php9.0会无效
// default => 1,
// };
// 获取类常量的值
var_dump(constant('self::' . $params['type']));
return true;
}
}