對 events.php 設(shè)置 namespace YourApp;
$worker->eventHandler = 'YourApp\Events';
啟動的時候,就會報錯 Waring: Events::onMessage is not callable
這是按照文檔來的,為什么還是報錯呢,這是什么原因
我也遇到了這個問題,經(jīng)過自己摸索,已經(jīng) 解決。
看了BusinessWorker類中,是通過is_callable函數(shù)來判斷是否是函數(shù)的。
源碼如下:
if (is_callable($this->eventHandler . '::onMessage')) {
$this->_eventOnMessage = $this->eventHandler . '::onMessage';
} else {
echo "Waring: {$this->eventHandler}::onMessage is not callable\n";
}
_這時我們心中的問題就是:那為何我們帶入命名空間后還是無法通過is_callable的檢測呢?_
答案:我們的命名空間沒有注冊,無法被BusinessWorker發(fā)現(xiàn)。
解決方法:在composer.json中注冊,代碼如下(Application就是我的命名空間,您自己的可以根據(jù)實際情況修改Applications(命名空間)文件夾要composer.json同級):"
autoload": {
"psr-4": {
"Applications\\": "Applications/"
}
}
?
嘗試了很多方式都沒解決,我妥協(xié)了,用原生引入文件,
include_once __DIR__ .'/../../../../application/push/controller/Events.php';
$directory=new Events();
if (method_exists($directory,'onMessage')) {
$this->_eventOnMessage = $this->eventHandler . '::onMessage';
} else {
echo "Waring: {$this->eventHandler}::onMessage is not callable\n";
}