PHP5 类范围怪癖

发布于 2024-09-28 19:05:54 字数 905 浏览 3 评论 0原文

嘿 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

流殇 2024-10-05 19:05:54

如果我理解正确,您正在寻找后期静态绑定。此功能至少需要 PHP 版本 5.3。

If I understand correctly, you are looking for Late Static Binding. This feature requires PHP version 5.3 at least.

如歌彻婉言 2024-10-05 19:05:54

您没有传递任何参数,但您的方法正在寻找它们。尝试

call_user_func("MyClass::search", $args);

这在 php 5.3.1 中有效,但 call_user_func("MyClass::search"); 不起作用

You're not passing any parameters, but your method is looking for them. Try

call_user_func("MyClass::search", $args);

This works in php 5.3.1, but call_user_func("MyClass::search"); doesn't

冷默言语 2024-10-05 19:05:54

试试这个:

call_user_func(array('MyClass', 'search'));

另请参阅 http://php.net/call_user_func 上的示例 #4

Try this:

call_user_func(array('MyClass', 'search'));

See also example #4 on http://php.net/call_user_func

童话 2024-10-05 19:05:54

你的代码看起来不错。如果有什么问题,我一定是错过了问题所在。看来您对 self:: 的调用完全在类的范围内!具体来说,还有一个静态作用域,这就是 self:: 的用途。

来自《PHP 对象模式与实践》第三版(一本很棒的书):

从同一个类中访问静态方法或属性
(而不是来自孩子),我会使用 self 关键字。自我是为了
类就像 $this 伪变量对于对象一样。所以从外面
StaticExample 类,我使用其类访问 $aNum 属性
名称:

StaticExample::$aNum;

在 StaticExample 类中,我可以使用 self 关键字:

class StaticExample {`
    静态公共$aNum = 0;

    静态公共函数 sayHello() {
        自我::$aNum++;
        print "你好 (".self::$aNum.")\n";
    }
}

所以,我不确定为什么此代码失败。也许是 PHP 错误?当我实际尝试在类的范围之外使用 self:: 时,我遇到了这个错误 - 我的错误如下所示:

public static function get_names() {
    $machine_names = self::get_machine_names();

    return array_map(function ($machine_name) {
        $service_settings = self::get_settings_by_machine_name($machine_name);
        return $service_settings . $machine_name;
        },
        $machine_names
    );
}

所以,我收到错误是因为我在闭包的范围内使用 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):

To access a static method or property from within the same class
(rather than from a child), I would use the self keyword. self is to
classes what the $this pseudo-variable is to objects. So from outside
the StaticExample class, I access the $aNum property using its class
name:

StaticExample::$aNum;

From within the StaticExample class I can use the self keyword:

class StaticExample {`
    static public $aNum = 0;

    static public function sayHello() {
        self::$aNum++;
        print "hello (".self::$aNum.")\n";
    }
}

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:

public static function get_names() {
    $machine_names = self::get_machine_names();

    return array_map(function ($machine_name) {
        $service_settings = self::get_settings_by_machine_name($machine_name);
        return $service_settings . $machine_name;
        },
        $machine_names
    );
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文