魔术常量 __CLASS__ 与 get_class_vars() 一起使用时出错
magic constant __CLASS__ used with get_class_vars() giving error
这里我有一个名为 vars 的私有变量,它用于将所有变量存储在 class.I 中,我使用 get_class_vars(__CLASS__)
获取所有 variables.But 它给出了解析错误。
Parse error: syntax error, unexpected '(', expecting ',' or ';' in C:\xampp\htdocs\practice\bal.php on line 5
如果我删除了 get_class_vars(__CLASS__)
代码 works.What 可能会导致问题??
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;
我认为你的问题出在这一行:
private $vars=get_class_vars( __CLASS__ );
定义 class var 时不能使用函数,您应该在构造函数中这样做:
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;
}
}
您不能将 'dynamic' 值分配给 class 属性 的 class 定义。您可以在构造函数中移动它们。另外我想你想要 $var
-> $vars
然后它是一个数组所以使用 print_r()
或 var_dump()
,像这样:
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;
有关 属性 的更多信息,请参阅手册:http://php.net/manual/en/language.oop5.properties.php
引自那里:
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.
这里我有一个名为 vars 的私有变量,它用于将所有变量存储在 class.I 中,我使用 get_class_vars(__CLASS__)
获取所有 variables.But 它给出了解析错误。
Parse error: syntax error, unexpected '(', expecting ',' or ';' in C:\xampp\htdocs\practice\bal.php on line 5
如果我删除了 get_class_vars(__CLASS__)
代码 works.What 可能会导致问题??
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;
我认为你的问题出在这一行:
private $vars=get_class_vars( __CLASS__ );
定义 class var 时不能使用函数,您应该在构造函数中这样做:
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;
}
}
您不能将 'dynamic' 值分配给 class 属性 的 class 定义。您可以在构造函数中移动它们。另外我想你想要 $var
-> $vars
然后它是一个数组所以使用 print_r()
或 var_dump()
,像这样:
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;
有关 属性 的更多信息,请参阅手册:http://php.net/manual/en/language.oop5.properties.php
引自那里:
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.