doukuang1897 2015-02-05 20:55
浏览 27
已采纳

与get_class_vars()一起使用的魔术常量__CLASS__给出错误[重复]

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>
  • 写回答

2条回答 默认 最新

  • duanniubeng2265 2015-02-05 20:58
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改