Jump to content

Namespace when using type declaration (aka type hinting)


Recommended Posts

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?

Edited by NotionCommotion
Link to comment
Share on other sites

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.