amphp/getting-started

Parse error: syntax error, unexpected '$socket' (T_VARIABLE) in D:\amphptest\server.php on line 14

DrLightman opened this issue · 4 comments

Hello,

trying the server.php from https://amphp.org/getting-started/tcp-chat/basic-echo-server but got a syntax error upong execution:

Parse error: syntax error, unexpected '$socket' (T_VARIABLE) in D:\amphptest\server.php on line 14

The code as taken from the page above:

<?php
require __DIR__ . "/vendor/autoload.php";

// Non-blocking server implementation based on amphp/socket.

use Amp\Loop;
use Amp\Socket\Socket;
use function Amp\asyncCall;

Loop::run(function () {
    $uri = "tcp://127.0.0.1:1337";

    $clientHandler = function (Socket $socket) {
        while (null !== $chunk = yield $socket->read()) { // line 14 is here
            yield $socket->write($chunk);
        }
    };

    $server = Amp\Socket\Server::listen($uri);

    while ($socket = yield $server->accept()) {
        asyncCall($clientHandler, $socket);
    }
});

I have PHP 7.4.2 on a Windows 10 Pro 64bit environment.

(also, could you please drop a Windows equivalent for nc localhost 1337 for the lesson at https://amphp.org/getting-started/tcp-chat/?

Thank you!

Spotted a typo, maybe? This:

use Amp\Socket\Socket;

shoud be this:

use Amp\Socket;

?

But still facing the same syntax error.

Looks like I managed to fix the parse error, and another one that presented after that, by surrounding the yield expression between parenthesis:

<?php
require __DIR__ . "/vendor/autoload.php";

// Non-blocking server implementation based on amphp/socket.

use Amp\Loop;
use Amp\Socket\Socket;
use function Amp\asyncCall;

Loop::run(function () {
    $uri = "tcp://127.0.0.1:1337";

    $clientHandler = function (Socket $socket) {
        while (null !== $chunk = (yield $socket->read())) { // ( ... ) added
            yield $socket->write($chunk);
        }
    };

    $server = Amp\Socket\Server::listen($uri);

    while ($socket = (yield $server->accept())) { // (...) added
        asyncCall($clientHandler, $socket);
    }
});

Now got another parse error in the library:

Parse error: syntax error, unexpected ':', expecting '{' in D:\amphptest\vendor\amphp\amp\lib\functions.php on line 21

Referring to this line of code:

    function coroutine(callable $callback): callable

Oh boy! Nevermind, I was running this on the other PHP 5.6.11 I've installed! On PHP 7.4.2 I got no errors whatsoever :)

Glad you figured it out.