4

PhpStorm is highlighting the following namespace as an error.

<?php

namespace App\Http\Controllers\Public;

Error: Expected: identifier

In general. Are reserved keywords like public, function, class not acceptable for namespacing?

MrMAG
  • 1,194
  • 1
  • 11
  • 34
  • 2
    Follow the links in this http://php.net/manual/en/reserved.php where http://php.net/manual/en/reserved.keywords.php does state `Public` as being one where it states *"....except that class may not be used as constant name."* so this may be the case. – Funk Forty Niner May 27 '17 at 17:16
  • Too bad. I hoped I could scope my web app into backend and public, namely. Seems I have to live with Frontend / Backend or something similar in that case. Thank you. – MrMAG May 27 '17 at 17:19
  • You're welcome. There might be a way around it, but I don't know it. – Funk Forty Niner May 27 '17 at 17:20

2 Answers2

15

The PHP manual hints that as of PHP 7.0.0 these keywords are allowed as property, constant, and method names of classes, interfaces and traits, except that class may not be used as constant name, which also incorporate the requested keyword public.

List of reserved keywords, which you can't use for namespacing:

__halt_compiler, abstract, and, array, as, break, callable, case, catch, class, clone, const, continue, declare, default, die, do, echo, else, elseif, empty, enddeclare, endfor, endforeach, endif, endswitch, endwhile, eval, exit, extends, final, for, foreach, function, global, goto, if, implements, include, include_once, instanceof, insteadof, interface, isset, list, namespace, new, or, print, private, protected, public, require, require_once, return, static, switch, throw, trait, try, unset, use, var, while, xor

A potential workaround to fix this issue is to add an extra character to the word or use the plural form of the word. Another opportunity would be to go one level up in the namespace and use the keyword as a prefix PublicFooController. (or even suffix, if you prefer that)

My intention was to scope my web app into public (for guests and visitors) and private (for private needs) area.
I went for namespace App\Http\Controllers\Frontend and namespace App\Http\Controllers\Backend instead of trying to incorperate the words public and private in to my namespacing, even if I personally think these generally fits better to my needs, because both have frontend and backend. But... Anyway, this keeps me off having trouble organizing my files and namespaces the way I would do, when I would use public and private.

MrMAG
  • 1,194
  • 1
  • 11
  • 34
  • 1
    *"I went for namespace App\Http\Controllers\Frontend and..."* You could go for `Admin` and `Site` (or alike) as an alternative. Or perhaps give names to your back end and front end and use them. – LazyOne May 29 '17 at 17:48
  • I called them General (Public) and Restricted (Private). – JohnDro Jun 15 '22 at 10:04
  • I'd suggest `open` = `public`, and `close` = `private` as alternatives. – Salim Ibrohimi Mar 30 '23 at 11:31
2

In PHP 8 this has changed. Namespaces are now single tokens and can contain reserved words. Here is an RFC available at https://wiki.php.net/rfc/namespaced_names_as_token

ThW
  • 19,120
  • 3
  • 22
  • 44