Syntax error, unexpected 'Interface' (T_INTERFACE), expecting identifier (T_STRING)

✔ Recommended Answer

My excuses, this is indeed documented behaviour, just not in the manual: see https://stackoverflow.com/a/75887334/13508 and https://wiki.php.net/rfc/namespaced_names_as_token


You may have found a corner case in PHP language parser. Interface in a PHP Keyword. But it isn't explicitly mentioned in the list of allowed/disallowed usages:

The following words cannot be used as constants, class names, or function names. They are, however, allowed as property, constant, and method names of classes, interfaces and traits, except that class may not be used as constant name.

These are valid:

use NotInterface\{    Parser\ZipFilenameParserInterface,    Query\UpdateLettersQueryInterface,};

Demo

use Interface\Parser\ZipFilenameParserInterface;use Interface\Query\UpdateLettersQueryInterface;

Demo

This is not:

use Interface\{    Parser\ZipFilenameParserInterface,    Query\UpdateLettersQueryInterface,};

Demo

Method #2

This error message typically occurs when you are trying to declare an interface, but you have not provided a name for the interface.

For example, consider the following code:

php
interface { public function myMethod(); }

This code will produce the following error message:

lua
PHP Parse error: syntax error, unexpected 'Interface' (T_INTERFACE), expecting identifier (T_STRING) in /path/to/file.php on line X

To fix this error, you simply need to provide a name for the interface. For example:

php
interface MyInterface { public function myMethod(); }

By providing a name for the interface, you are specifying the identifier that is expected by the PHP parser, and the error message should no longer occur.

Comments

Most Popular

PhpStorm, return value is expected to be 'A', 'object' returned

Remove Unicode Zero Width Space PHP

Laravel file upload returns forbidden 403, file permission is 700 not 755