php namespaces falling back to global using variable not working - php

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

Related

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.

Can't use "const" and "define" in PHP class

I am pulling my hair out on this one. Pretty new at PHP but this is so basic I just can't figure out where the problem is. Using the code snippet below as an example:
class LG_Activity_Processor {
// Activity Types
const STATUS_DRAFT = 'draft';
const STATUS_PUBLISH = 'publish';
...
private $STATUS_FUTURE = 'future';
define ("STATUS_PRIVATE" , 'private');
I had originally intended to just use the "const" construct as the variables are fully defined prior to run time and I just think the syntax is prettier than that ugly "define". The problem is while the definition of the const gives no errors, whenever I refer to the constant later in the class I get the following error message:
PHP Notice: Use of undefined constant STATUS_PUBLISH - assumed 'STATUS_PUBLISH'
Huh? Just to be clear, here's the syntax I used to reference the "const":
$core_fields ['post_status'] = STATUS_PUBLISH;
I even tried:
$core_fields ['post_status'] = $this->STATUS_PUBLISH;
No love. I then entered a state of despair and eventually tried "define". Same calling syntax but I changed the definition syntax to that as illustrated above for "STATUS_PRIVATE". This made things far worth as I now had a fatal error on the define line that looked like this:
PHP Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION
I gave up. I finally just defined the variable as a private variable (as in the example of STATUS_FUTURE) and then referred to it as:
$core_fields ['post_status'] = $this->STATUS_PUBLISH;
That works just like you'd expect it to but I can't help but feeling I've been cheated out of doing it the right way. Any ideas on how to make my code whole again?
You should do LG_Activity_Processor::STATUS_DRAFT when accessing it.
Take a look at the PHP manual, it gives you clear examples.
http://php.net/manual/en/language.oop5.constants.php

PHP error, concatenation in array() in member initialiser

I'm getting an error with the following code:
public $arr = array('email' => 'admin#' . str_replace('http://', '', SERVER_ROOT));
Parse error: syntax error, unexpected '.', expecting ')'
Am I being really stupid? Surely I can concatenate strings here?
This is a variable declared in a class.
You cannot initialize class attributes with an expression. You have to do that in the constructor or use a fixed value, like a regular string.
This is an error, you can't initialize a property like this
Properties
Class member variables are called "properties". You may also see them
referred to using other terms such as "attributes" or "fields", but
for the purposes of this reference we will use "properties". They are
defined by using one of the keywords public, protected, or private,
followed by a normal variable declaration. This declaration may
include an initialization, but this initialization must be a constantvalue--that is, it must be able to be evaluated at compile time andmust not depend on run-time information in order to be evaluated.

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