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_STRINGs joined together by T_NS_SEPARATORs (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.
Related
When setting a namespace to "App\Case" I'm being thrown the following error:
syntax error, unexpected 'Case' (T_CASE), expecting identifier (T_STRING)
Code line in reference:
use App\Case;
Changing the word from "case" to something else fixes this. But from what I understand this is a valid namespace name and should not collide with the reserved keyword 'case' in PHP.
I encountered this issue whilst using Laravel migrations on Homestead CLI, but I don't believe this is relevant to the problem
Is this a bug or an invalid name in the namespace?
They're reserved for a reason because in the first scenario above, PHP wouldn't be able to tell the difference between you defining an array or initialising a class of the same name, so it throws an error. There's no way round this like in MySQL, for example, where you can escape reserved words with a backtick. So in PHP you're forced to change the name, you don't have to change it much, one char will do as long as you're not using the exact same name as a reserved word.
I have a model called Class and User.
my User model has this line of code.
use App\Class;
its giving me an error
"syntax error, unexpected 'Class' (T_CLASS), expecting identifier (T_STRING)"
but when I use other names its just fine.
could someone tell me why its not possible to use the word Class as a classname
Class is a reserved word for php
php reserved keywords
Try renaming it?
Hope it helps :)
I have two files:
index.php
$a = "a";
namespace.php
namespace tom\anderson\s;
include 'index.php';
echo \$a;
This does not work and outputs this error message: Parse error: syntax error, unexpected '$a' (T_VARIABLE), expecting identifier (T_STRING) in...
Why is this? Any references to official documentation would help!
From PHP docs:
PHP Namespaces provide a way in which to group related classes, interfaces, functions and constants.
As you are using to group variables, the error is being triggered.
Variables do no belong to a namespace and exist in the global scope.
To get program work slash before $a must be removed.
From php documentation. Defining namespaces
Although any valid PHP code can be contained within a namespace, only the following types of code are affected by namespaces: classes (including abstracts and traits), interfaces, functions and constants.
http://php.net/manual/en/language.namespaces.definition.php
class List {
public function hello()
{
return "hello";
}
}
$list = new List;
echo $list::hello();
Gives Error:
PHP Parse error: syntax error, unexpected 'List' (T_LIST), expecting identifier (T_STRING) in /home/WtGTRQ/prog.php on line 3
Changing "List" to "Lizt" fixes the issue.
I sadly understand that Php functions are case-insensitive, but I really don't want to make a List object a Lizt object... Is there some way to circumvent this without renaming my class?
List is a restricted PHP word.
You cannot use any of the following words as constants, class names, function or method names.
http://www.php.net/manual/en/reserved.keywords.php
--
To answer your question, you will have to change your list class name to something else. MyList, CarList, Listing, etc..
I would like to define a class named "List" like this:
class List
{
}
PHP gives the following error at the file of the class definition: "Parse error: syntax error, unexpected T_LIST, expecting T_STRING"
Apparently there is a php built-in function named "list()" that the parser is reading here instead of my class definition even though the line starts with the keyword class
Since I don't use the built-in function anywhere in my project I would like to "remove/disable" it, so I can use my class named "List".
Is this possible in php and how?
list is a reserved word as it is a language construct (not actually a function), so no, you cannot disable it. Try being more specific with your class name, e.g., ObjectList or AbstractList.
The class name can be any valid label which is a not a PHP reserved
word.
List of PHP reserved words says list is one of those words.