开发者

PHP5 Class scope quirks

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?


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


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


Try this:

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

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜