In a Laravel 4 application, is it possible to create a controller in namespace called Public
? Like this:
<?php namespace Public;
class MyController extends \BaseController {
}
Doing this gives me an error:
syntax error, unexpected 'Public' (T_PUBLIC), expecting identifier (T_STRING) or \ (T_NS_SEPARATOR) or '{'
However, if I change the namespace to PublicControllers
, it works fine. Does that means Public
is a reserved word that can't be used as a namespace?
public
is a reserved word in PHP:
These words have special meaning in PHP. Some of them represent things which look like functions, some look like constants, and so on - but they're not, really: they are language constructs. You cannot use any of the following words as constants, class names, function or method names. Using them as variable names is generally OK, but could lead to confusion.
While namespaces aren't specifically mentioned here we can look at the PHP grammar and see that namespaces are expected to be made from T_STRING
s joined together by T_NS_SEPARATOR
s (backslashes). Since public
has its own token type (T_PUBLIC
, which is mentioned in your error message) it is not an appropriate choice.
Note that this has nothing to do with Laravel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With