Laravel 5 - Using Class word as model name - php

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 :)

Related

PHP namespace containing word "Case"

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.

PHP namespace called 'Public'

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.

Trying to make a class called "List", but the list() function is breaking it

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..

How to define a class with same name as built-in function?

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.

Why is specifying a defined constant inside a class constant throwing an error?

Here is part of my code
define('DIR_APP', 'app/');
class Questions
{
const QUESTIONS_FILE = DIR_APP . 'questions.xml';
}
Seems when I take the define()'d constant out of my class constant declaration, it works fine. But when I add it it throws this error:
Parse error: syntax error, unexpected '.', expecting ',' or ';' in /home/public_html/app/classes/Questions.class.php on line 7
So how can I get my define()'d constant in there? I assume it is not correctly looking up the DIR_APP thinking it might of been defined within the class. How do I get it to resolve it globally?
Thank you
It can't be done.
Quote from http://www.php.net/manual/en/language.oop5.constants.php :
"The value must be a constant
expression, not (for example) a
variable, a class member, result of a
mathematical operation or a function
call"
I think you could
define('QUESTIONS_FILE', DIR_APP .
'questions.xml');
but that is global.
Never tried that before. Zend Studio is giving me an issue at the moment to look at it myself. How about trying this though...
const QUESTIONS_FILE = constant("DIR_APP") . 'questions.xml';

Categories