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.
Related
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
I want the variable to be parsed within the array so when I echo $head['meta_title'] , lol is displayed. I have tried wrapping it in double quotes but that doesn't seem to work either, is there any way round this?? Thanks!
I am getting either unexpected T_VARIABLE and when I use double quotes I get unexpected ""
$meta_title = "lol";
public $head = array
(
"title" => "blah",
"meta_title" => $meta_title,
"meta_content" => $meta_content
);
You cannot use an expression to initialize a class property. The values of the two variables are not known until runtime, and therefore can't be used in the declaration. Instead, define them in the constructor.
public $head = array
(
// The title as a string literal is ok...
"title" => "blah",
"meta_title" => NULL,
"meta_content" => NULL
);
// Pass them to the constructor as parameters
public function __construct($meta_title, $meta_content)
{
// Initialize them in the constructor.
$this->head['meta_title'] = $meta_title;
$this->head['meta_content'] = $meta_content;
}
From the docs
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 constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
If this is within an object you cannot assign a variable this way. You have to set it using the __construct-method.
The public needs to be removed before $head. I've created an example in PHP Sandbox.
http://sandbox.onlinephpfunctions.com/code/d17bc90e1291f6a3b23537984df755e40446add6
I'm trying to insert a static variable in an array like this :
static $datas = array(
'link' => config::$link
);
But i'm having this error
Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING
I discovered PHP doc say that :
Like any other PHP static variable, static properties may only be
initialized using a literal or constant; expressions are not allowed.
So while you may initialize a static property to an integer or array
(for instance), you may not initialize it to another variable, to a
function return value, or to an object.
But I'm sure there is a way to do that, any suggestion ?
No, there is no workaround. static variables and properties can only be initialized with constant values. That means literals or constants. Variables, static or not, cannot be used, period. You have to assign a variable value later using procedural code somewhere.
When I call my static method by static::some_method(); it gives me the following error:
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting T_VARIABLE in /some/path/SomeClass.class.php on line 15
If I replace static with the class name it works of course, but what is the correct way to call a static method without using the classname?
If you are within the context of the class then
self::method();
static::method();
...will both work, with different behaviors related to late static binding.
If you are not in the context of a class, then you need to use the classname the method belongs to:
SomeClass::method();
Otherwise you'll get that goofy hebrew error, T_PAAMAYIM_NEKUDOTAYIM, which means "double colon" in English.
Use
ClassName::some_method()
to invoke static method (not using static keyword) or, if you are inside one that class, use
self::some_method()
where self is a keyword (i.e. inside another method).
Here is a minimal test case I've isolated:
<?php
class What {
public $foo = range(0,5);
}
?>
I have no idea why this produces an error:
PHP Parse error: syntax error, unexpected '(', expecting ',' or ';' in TestCase.php on line 4
Using array() works.
Using PHP 5.3.3 (bundled with OS X).
You can only assign constant values in that context. You'll have to initialize your $foo in a constructor if you want to use the return value of a function.
<?php
class What {
public $foo;
public function __construct() {
$this->foo = range(0,5);
}
}
?>
BTW: As others have pointed out, array() is not a function. It's a language construct.
Array isn't a function, it's a language construct. That's why it's allowed.
Class member variables are called "properties" ... may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
http://www.php.net/manual/en/language.oop5.properties.php
Check this link as a similar problem was sent and had been solved :-
http://www.phpbuilder.com/board/showthread.php?t=10366062
Also you can see the examples of using range() function in PHP.Net Manual although I think the problem may be in the variable $foo and public keyword as you there may be a type mismatch or a conflict beteen the version of the PHP function and your running PHP version.
I hope this answer helps you..