只使用一个简单的文件,我就可以让elephant.io工作,但如果我试图将它集成到一个类中,它就无法工作。
它不起作用的原因是它说我不能在类或函数中使用use elephant ...
。
如何将elephant.io集成到现有的类中?
更具体地说,我正在尝试将elephant.io集成到codeigniter框架中。
function tester() {
use ElephantIOClient,
ElephantIOEngineSocketIOVersion1X;
require __DIR__ . '/vendor/autoload.php';
$client = new Client(new Version1X('http://www.textbasedmafiagame.com:8080'));
$client->initialize();
$client->emit('broadcast2', ['foo' => 'utførte et biltyveri']);
$client->close();
}
没有得到任何回应或结果,但我确实得到了:
use not allowed inside a function
和
Parse error: syntax error, unexpected 'use'
根据错误状态,use
关键字必须在文件的最外层范围(全局范围)或命名空间声明内声明。试试
<?php
use ElephantIOClient, ElephantIOEngineSocketIOVersion1X;
require __DIR__ . '/vendor/autoload.php';
class foo{
public function tester() {
$client = new Client(new Version1X('http://www.textbasedmafiagame.com:8080'));
$client->initialize();
$client->emit('broadcast2', ['foo' => 'utførte et biltyveri']);
$ client->close();
}
}