NotionCommotion Posted May 13, 2017 Share Posted May 13, 2017 Why does the first example work, but not the second example. Thank you namespace Testing\Server; //... $socket = new \React\Socket\Server($this->host['url'].':'.$this->host['port'], $loop); $socket->on('connection', function (\React\Socket\ConnectionInterface $stream) { namespace Testing\Server; //... $socket = new \React\Socket\Server($this->host['url'].':'.$this->host['port'], $loop); $socket->on('connection', function (ConnectionInterface $stream) { Also, if I wasn't using namespace Testing\Server in this script, would things change? Link to comment Share on other sites More sharing options...
Jacques1 Posted May 13, 2017 Share Posted May 13, 2017 Plain, unqualified identifiers are resolved relative to the current namespace, so ConnectionInterface means Testing\Server\ConnectionInterface. Which is obviously not what you mean. You want \React\Socket\ConnectionInterface. I'm not even sure what exactly you expected. How is PHP supposed to figure out that ConnectionInterface belongs to some other namespace? Link to comment Share on other sites More sharing options...
NotionCommotion Posted May 13, 2017 Author Share Posted May 13, 2017 I agree with you that it obviously was not what I want and there is no way PHP can figure out what I want without me telling it. The example I took it from was from https://github.com/reactphp/socket#quickstart-example. My confusion is that the current namespace in this example is presumably \, yet they hint with only ConnectionInterface which would be \ConnectionInterface, not \React\Socket\ConnectionInterface. What am I missing? $loop = React\EventLoop\Factory::create(); $socket = new React\Socket\Server('127.0.0.1:8080', $loop); $socket->on('connection', function (ConnectionInterface $conn) { $conn->write("Hello " . $conn->getRemoteAddress() . "!\n"); $conn->write("Welcome to this amazing server!\n"); $conn->write("Here's a tip: don't say anything.\n"); $conn->on('data', function ($data) use ($conn) { $conn->close(); }); }); $loop->run(); Link to comment Share on other sites More sharing options...
Jacques1 Posted May 13, 2017 Share Posted May 13, 2017 You're interpreting too much into this example code. Chances are it was never actually tested, or they've copied and pasted it from a different context (read: there were in a different namespace without explicitly mentioning it). It doesn't matter. The rules are the rules. Link to comment Share on other sites More sharing options...
requinix Posted May 13, 2017 Share Posted May 13, 2017 Check the history for README.md: the ConnectionInterface hint was added in a commit that wasn't originally about documentation, so it's likely the author just didn't consider the changes they were making. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.