This question already has an answer here:
Here i have a private variable called vars which is used to store all the variables inside a class.I am using get_class_vars(__CLASS__)
to get all the variables.But it gives a parse error.
Parse error: syntax error, unexpected '(', expecting ',' or ';' in C:\xampp\htdocs\practice\bal.php on line 5
if i removed the get_class_vars(__CLASS__)
the code works.What might be causing the problem??
class lol{
static private $message='i am shimantta !! ';
private $msg='new';
private $vars=get_class_vars( __CLASS__ );
public function __get($var){
echo $this->$var;
}
}
$lol1=new lol();
$lol1->vars;
</div>
You can't assign 'dynamic' values to class property's in class definition. You can move them in the constructor. Also i think you want $var
-> $vars
and then it is an array so use print_r()
or var_dump()
, like this:
class lol {
static private $message = "i am shimantta !! ";
private $msg = "new";
private $vars= "";
public function __construct() {
$this->vars = get_class_vars( __CLASS__);
}
public function __get($vars){
print_r($this->$vars);
}
}
$lol1 = new lol();
$lol1->vars;
For more information about property's see the manual: http://php.net/manual/en/language.oop5.properties.php
And a quote from there:
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.
I think your problem is in this line:
private $vars=get_class_vars( __CLASS__ );
You cannot use a function when defining a class var, you should do that in the constructor:
class lol{
private static $message='i am shimantta !! ';
private $msg='new';
private $vars;
function __construct() {
$this->vars=get_class_vars( __CLASS__ );
}
public function __get($var){
echo $this->$var;
}
}