0

I have PHP (7.1) code below. Upon executing that code I'm getting the error below:

Parse error: syntax error, unexpected 'Interface' (T_INTERFACE), expecting identifier (T_STRING) or function (T_FUNCTION) or const (T_CONST) or \ (T_NS_SEPARATOR) in your code on line 7

<?php

declare(strict_types=1);

namespace CommandBus\Handler;

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

final class ZipFileGeneratorCommandHandler
{
    // the rest of the code
}

Googling didn't help much. I couldn't ChatGPT this thing 'cause I don't have access to ChatGPT :(.

user3783243
  • 5,368
  • 5
  • 22
  • 41
Salim Ibrohimi
  • 1,351
  • 3
  • 17
  • 35
  • 2
    https://stackoverflow.com/questions/44219644/php-reserved-keywords-allowed-in-namespace-public-private-default could help. Also, PHP 7.1 is horribly outdated, and I hope you don't use it for any production system – Nico Haase Mar 30 '23 at 10:45
  • 2
    `I couldn't ChatGPT this thing`....well it's not a search engine or knowledge consultant anyway, so I wouldn't worry. It can talk to you, but there's no guarantee it will say anything correct. Its purpose is to be able to hold a conversation which sounds linguistically coherent and realistic, not to provide accurate information. (And the same applies to any program code it generates...it can produce stuff which _looks like_ PHP code do a certain task, based on other examples of PHP code it's seen...but it will often be incorrect or illogical (possibly in a quite subtle way).) – ADyson Mar 30 '23 at 10:47
  • 2
    Anyway the problem is that Interface is a reserved word, you can't use it as a namespace. – ADyson Mar 30 '23 at 10:48
  • 3
    Out of curiosity, why has it become a trend to mention that ChatGPT hasn't solved the problem? It doesn't add any useful information to questions, and it kind of gives a "reluctant to ask here" tone. Regarding your question, I think you've found an edge case in PHP language parser. `Interface` works if you split your grouped namespace differently. – Álvaro González Mar 30 '23 at 10:49
  • @ADyson, and Nico Haase - "Interface" is a reserved word. Thanks, guys! – Salim Ibrohimi Mar 30 '23 at 10:51
  • 1
    @ÁlvaroGonzález depressingly I suspect that some people have decided that "ask a chatbot" is a valid substitute for doing some actual research. – ADyson Mar 30 '23 at 10:52
  • @ÁlvaroGonzález - yeah, about that. Some folks on the Internet suggest asking ChatGPT, so I wanted to answer them before they even suggest that to me :) – Salim Ibrohimi Mar 30 '23 at 10:54
  • 1
    `Some folks on the Internet`... I wouldn't take much notice of those folks. I mean, it's one option you can consider as part of your research, but you don't need to consider it specifically. Stackoverflow expects that you do an adequate amount of research before asking (as per [ask]) but it doesn't specify how you do it. If you tried something, of course we're interested in that, and we _might_ end up being interested in the source of the info, but there's no particular requirement here to search via chatbots specifically. – ADyson Mar 30 '23 at 10:57
  • @ÁlvaroGonzález you don't think it's a duplicate of https://stackoverflow.com/questions/44219644/php-reserved-keywords-allowed-in-namespace-public-private-default, then? – ADyson Mar 30 '23 at 10:57
  • 1
    @ADyson Not if I read documentation correctly. Perhaps the manual has changed since that other question was answered, I don't know. – Álvaro González Mar 30 '23 at 10:59
  • @NicoHaase - yeah, version 7.1 is outdated. I'm planning on migrating to version 8.0. – Salim Ibrohimi Mar 30 '23 at 11:02
  • 2
    Please directly plan to migrate to a version that receives some more support - 8.0 has left active support some months ago, and security support will end in Nov 2023 – Nico Haase Mar 30 '23 at 11:48

1 Answers1

2

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

Álvaro González
  • 142,137
  • 41
  • 261
  • 360