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.
Related
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.
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 :)
So... This is more of a curiosity, not a real problem. I don't get this:
<?php
define('EMPTY', 1);
echo EMPTY;
Results in: Parse error: syntax error, unexpected ';', expecting '('
My first thought is that empty is reserved for the empty function. But then I tried:
<?php
define('IS_ARRAY', 1);
echo IS_ARRAY;
Result: 1
So... I don't get it. Why empty can't be used as constant while for example is_array, die, etc. can.
empty is not a function but a language construct (more like an operator than a function), which means it's resolved by the parser at parse time and not by the runtime. It's a reserved keyword and you cannot use it for anything yourself like function names, or constants.
The reason empty cannot be a regular function is because it behaves differently. empty($undefined) would first resolve $undefined, throw an error, then pass null to empty; which is exactly the case empty is supposed to prevent, so it cannot play by the normal function invocation rules.
empty is a reserved keyword in PHP. You cannot use any reserved words as constants, class names, function or method names as these words have special meaning in PHP. Use any other name as constant.
You can definately define "EMPTY" as a constant. The problem is you can't use it directly.
<?php
define("EMPTY", "valueForEmpty");
echo EMPTY; // Wont work because empty is a keyword and PHP doesn't know that you mean the constant.
echo constant("EMPTY"); // Works
Note that the reserved keywords are also case insensitive, and they can't be defined as constant or redefined using runkit_function_redefine();
You can find here the exhaustive list of reserved keywords
I can see in PHP 5.3.2 there is an ArrayObject class. Is it possible to declare a new object named Array
that extends the ArrayObject. But Im not sure if 'Array' is a reserved keyword or should I use my own prefix i.e XArray(), MyArray etc...
What I would like to have is
class Array extends ArrayObject {
...my own code
}
All you'd have to do is try to run:
<?php class Array {}
And you'd see:
Parse error: syntax error, unexpected 'Array'
So the answer is, no, you cannot. You'll need to use a different name.
If it works right now, it may just happen that they do create an Array class in the future. It is best just to stay away from names that are so obviously logical for the language to define.
edit: As Matthew pointed out, it doesn't work. Seeing his answer made me realize why it doesn't work: array is a keyword in php (allowing you to make an array literal) and keywords are case insensitive in php.
Im not sure if 'Array' is a reserved keyword
That's pretty easy to check. PHP's list of reserved keywords has "array()" listed front and center.
That page also lists the following guidance:
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.
So no, you can't. You need to rename your class to something else, and your name must be differentiated by more than just letter-casing since PHP class names are case-insensitive.
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.