php - 在回调函数中调用命名空间类的静态方法
问题描述
遇到了我在编码过程中遇到的最奇怪的错误。
我正在使用:带有文件驱动程序的 laravel 棘轮 websocket Laravel Cache
我正在尝试使用来自工匠命令类的 laravel 缓存从其闭包函数中缓存棘轮 websocket 响应消息。
当我在 websocket 响应上使用 var_dump 时,我会在终端上打印出所有消息。但是当我尝试保存在缓存中时,它返回 true 但缓存为空。不被存储,不显示错误消息。
这是我的代码:
use Illuminate\Support\Facades\Cache as Cache;
class Ticker extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'daemon:t';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Listens to websockets t';
/**
* Create a new command instance.
*
* @return void
*/
protected $cah;
public function __construct(Cache $Cache)
{
$this->cah = new $Cache();
parent::__construct();
}
public function mini(callable $callback)
{
// phpunit can't cover async function
\Ratchet\Client\connect('wss://stream......')->then(function ($ws) use ($callback) {
$ws->on('message', function ($data) use ($ws, $callback) {
$json = json_decode($data, true);
return call_user_func($callback, $this->cah, $json);
});
$ws->on('close', function ($code = null, $reason = null) {
// WPCS: XSS OK.
echo "....: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL;
});
}, function ($e) {
// WPCS: XSS OK.
echo "....: Could not connect: {$e->getMessage()}" . PHP_EOL;
});
}
// save
public function saveT($t){
//$this->alert($t);
try{
foreach ($t as $obj) {
$this->cah::put($obj['s'], $obj, now()->addSeconds(5));
}
// used in testing
//print_r($t);
} catch (\Exception $exception) {
$this->alert($exception->getMessage());
}
}
/**
* Execute the console command.
*
* @return mixed
* @throws \Exception
*/
public function handle()
{
$this->mini(function ($api,$json){
// saving directly though the $api object
$api::put(['s' => $json], now()->addSeconds(10));
// when savingnthrough saveT function
// $this->saveT($json);
try{
// call_user_func_array(array($this->cah::class,'put'),array('test','tested',now()->addSeconds(5)));
} catch (\Exception $exception) {
$this->alert($exception->getMessage());
}
},);
}
}
解决方案
您发布的示例代码是一堆相互冲突的约定、名称和引用。一旦你理清了你真正需要的是什么,这里是实际构造和调用可调用对象的正确形式。
namespace foo {
class Cache {
public static function put_static($a, $b, $c='optional?') {
echo "put_static: $a $b $c\n";
}
public function put_instance($a, $b, $c='optional?') {
echo "put_instance: $a $b $c\n";
}
}
}
namespace {
use foo\Cache as Cache;
$c = new Cache();
$callback_static = [Cache::class, 'put_static'];
$callback_instance = [$c, 'put_instance'];
$callback_instance_static = [$c::class, 'put_static'];
$vars_a = ['a', 'b'];
$vars_b = ['a', 'b', 'c'];
call_user_func_array($callback_static, $vars_a);
call_user_func_array($callback_static, $vars_b);
call_user_func_array($callback_instance, $vars_a);
call_user_func_array($callback_instance, $vars_b);
call_user_func_array($callback_instance_static, $vars_a);
call_user_func_array($callback_instance_static, $vars_b);
}
输出:
put_static: a b optional?
put_static: a b c
put_instance: a b optional?
put_instance: a b c
put_static: a b optional?
put_static: a b c
您还可以使用 splat 运算符保存一些击键以...
将数组解压缩为参数,因此call_user_func_array($func, $args)
变为$func(...$args)
参考:
推荐阅读
- r - R中的功能调用问题
- java - Anylogic - 停车延迟块
- mysql - 创建新表并插入具有 column1=kermit(value) 和 status=completed(value) 的列
- sql - 和/或 SQL 语句
- c# - 在没有聚合的情况下转置 LINQ 中的行和列
- javascript - 当 updateViewDate 为 false 时,如何在 bootstrap-datepicker 中更改月份视图?
- c# - 跳转点搜索结果不正确
- foreign-keys - 如何在 Blazor 客户端的列表中显示外键名称
- swift - 如何仅使用文本字段使按钮可点击到另一个视图控制器
- pyqt5 - 如何从 PyQT5 窗口访问 PyQGS 地图画布