donglan6967
2015-03-19 17:45
浏览 66
已采纳

使用变量类名和静态方法时出错

Running PHP 5.4, so I wasn't expecting this, but I'm encountering the following error:

Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)

Assume you have a variable of stdClass setup as follows:

$this->variable = new stdClass();

$this->variable->other = array('class' => 'helloworld');

Now, assume you want to access a static method of class helloworld:

// Standard call
$x = helloworld::my_static_method();

// Call with variable class name
$x = $this->variable->other['class']::my_static_method();

When calling the above using the variable class name, I receive the parsing error. What's odd, is that if I do the following, no error is presented:

$class = $this->variable->other['class'];

$x = $class::my_static_method();

To me this seems very odd, can anyone think of a reason why the class name isn't resolving correctly when using the first example versus the second?

图片转代码服务由CSDN问答提供 功能建议

运行PHP 5.4,所以我没想到这一点,但我遇到了以下错误:

 解析错误:语法错误,意外'::'(T_PAAMAYIM_NEKUDOTAYIM)
   
 
 

假设您有一个 stdClass 设置如下:

  $ this-> variable = new stdClass(); 
 
 $ this-> variable-> other  = array('class'=>'helloworld'); 
   
 
 

现在,假设您要访问类 helloworld

  //标准调用
 $ x = helloworld :: my_static_method(); 
 
 //使用变量类名调用
 $ x = $ this  - > variable->其他['class'] :: my_static_method(); 
   
 
 

使用变量类名调用上面的内容时,我会收到解析 错误。 奇怪的是,如果我执行以下操作,则不会出现错误:

  $ class = $ this-> variable-> other ['class']; \  n 
 $ x = $ class :: my_static_method(); 
   
 
 

对我来说,这似乎很奇怪,任何人都可以想到为什么班级名称不是' 使用第一个例子与第二个例子时正确解析?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题