PHP5 类范围怪癖
嘿 PHP 大师。我遇到了一些奇怪的类范围问题,这些问题显然与 php 中的一些怪癖有关。谁能告诉我什么异常情况可能会出现以下错误...
致命错误:无法访问 self:: 当 MyClass.php 中没有活动的类作用域时第 5 行
现在,显然如果我在课堂之外使用 self:: ,我会收到错误......但我没有。这是情况的简化版本...
//file1
class MyClass{
public static function search($args=array()){
$results = MyDbObject::getQueryResults("some query");
$ordered_results = self::stack($results); //Error occurs here
return $ordered_results;
}
public static function stack($args){
//Sort the results
return $ordered_results;
}
}
//file 2
include_once("MyClass.php");
$args = array('search_term'=>"Jimmy Hoffa");
$results = MyClass::search($args);
鉴于此设置,我怎样才能得到上述错误?这是我到目前为止发现的...
MyClass::search($args) //does not give the error (usually)
call_user_func("MyClass::search"); // this gives the error!
还有其他情况吗?
Hey php gurus. I'm running into some bizarre class scope problems that clearly have to do with some quirk in php. Can anyone tell me what out-of-the-ordinary situations might give the following error...
Fatal error: Cannot access self:: when no class scope is active in MyClass.php on line 5
Now, obviously if I were to use self:: outside of the class, I'd get errors... but I'm not. Here is a simplified version of the situation...
//file1
class MyClass{
public static function search($args=array()){
$results = MyDbObject::getQueryResults("some query");
$ordered_results = self::stack($results); //Error occurs here
return $ordered_results;
}
public static function stack($args){
//Sort the results
return $ordered_results;
}
}
//file 2
include_once("MyClass.php");
$args = array('search_term'=>"Jimmy Hoffa");
$results = MyClass::search($args);
given this setup how can I get the error above? Here is what I've found so far...
MyClass::search($args) //does not give the error (usually)
call_user_func("MyClass::search"); // this gives the error!
Any other situations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我理解正确,您正在寻找后期静态绑定。此功能至少需要 PHP 版本 5.3。
If I understand correctly, you are looking for Late Static Binding. This feature requires PHP version 5.3 at least.
您没有传递任何参数,但您的方法正在寻找它们。尝试
这在 php 5.3.1 中有效,但
call_user_func("MyClass::search");
不起作用You're not passing any parameters, but your method is looking for them. Try
This works in php 5.3.1, but
call_user_func("MyClass::search");
doesn't试试这个:
另请参阅 http://php.net/call_user_func 上的示例 #4
Try this:
See also example #4 on http://php.net/call_user_func
你的代码看起来不错。如果有什么问题,我一定是错过了问题所在。看来您对 self:: 的调用完全在类的范围内!具体来说,还有一个静态作用域,这就是 self:: 的用途。
来自《PHP 对象模式与实践》第三版(一本很棒的书):
所以,我不确定为什么此代码失败。也许是 PHP 错误?当我实际尝试在类的范围之外使用 self:: 时,我遇到了这个错误 - 我的错误如下所示:
所以,我收到错误是因为我在闭包的范围内使用 self:: 。要修复该错误,我可以在闭包之前调用 self::get_settings_by_machine_name(),并使用
use
将结果传递到闭包的作用域。不确定您的代码中发生了什么。
Your code seems fine. If there's something wrong with it, I must be missing the problem. It appears that your call to self:: is totally within the scope of a class! And a static scope, specifically, which is what self:: is for.
From the 3rd Edition of PHP Objects Patterns and Practice (an awesome book):
So, I am not sure why this code was failing. Perhaps a PHP bug? I came upon this error when actually trying to use self:: outside of the scope of a class-- my error looked like this:
So, I get the error because I use self:: within the scope of the closure. To fix the error, I could make that call to self::get_settings_by_machine_name() before the closure, and pass the results to the closure's scope with
use
.Not sure what was happening in your code.