I am using PHP 5.3 on Ubuntu. I want to share a pair of code snippets and want to know the reason of such contradiction in order to initialize associated array.
Case 1: A part of class
class ui_template extends ui\ui_component{
private $path = __DIR__."/templates";
private $properties_template = array('path' => __DIR__.'/templates', 'file' => 'mytemplate.php', 'engine' => 'php'); // Parse error: syntax error, unexpected '.', expecting ')'
private $properties_template = array('path' => $path, 'file' => 'mytemplate.php', 'engine' => 'php'); // Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION
Case 2: A part of simple script
$path = __DIR__ . "/templates";
$temp = new widget\ui_template('new_template');
$temp->extend_template(NULL, array('path' => __DIR__."/templates")); // works fine
$temp->extend_template(NULL, array('path' => $path)); // works fine
-
In the first case at line#3 that is strange to report an error on
.while definingpath, means concatenation is not allowed there. While in case 2 nothing such error. -
In the first case at line#4 a variable is also not allowed to initialized
path
You can’t use computed values when declaring class properties. They must be constant (literal string, number, etc.). Docs: