Hi
why does the following syntax generate an error:
<?php
class Foo
{
protected $my_static = $var . 'foo';
}
Parse error: syntax error, unexpected T_VARIABLE in /var/www/test.php on line 4
Hi
why does the following syntax generate an error:
<?php
class Foo
{
protected $my_static = $var . 'foo';
}
Parse error: syntax error, unexpected T_VARIABLE in /var/www/test.php on line 4
Hello,
you can’t declare attributes of a class in that way. You can assign values to attributes of class using constructor:
class Foo
{
protected $my_static;
public function __construct($my_static){
$this->my_static = $my_static;
}
public function get_my_static(){
return $this->my_static;
}
}
$var = 10;
$foo = new Foo($var);
echo $foo->get_my_static();
You can assign value to attributes in this way as well:
class Foo
{
protected $my_static = 10;
protected $my_static = 'some string';
}
If you try to use any “operation” like . or if you try to assing value to the attribute by calling a function etc., then you will always run into PHP Error.
function test(){return 10;};
class Foo
{
protected $my_static = test();
protected $my_static = 'abc'.'def';
}
Thank u so much sir…that really helped!!!