The scope of the PHP5 class

Hi guru php. I am facing some weird class issues that are clearly related to some quirk in php. Can someone tell me what unusual situations can lead to the following error ...

Fatal error : Unable to access itself :: if the class of the class is not active in MyClass.php on line 5

Now, obviously, if I used self :: outside the class, I would get errors ... but I do 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 setting, how can I get the error above? Here is what I have found so far ...

MyClass::search($args) //does not give the error (usually)
call_user_func("MyClass::search"); // this gives the error!

Any other situations?

+3
source share
4 answers

, . , , PHP 5.3.

+1

, .

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

php 5.3.1, call_user_func("MyClass::search");

+1

:

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

. # 4 http://php.net/call_user_func

0

. - , . , :: ! , , , :: .

3- PHP ( ):

( ), self. , $this . StaticExample, $aNum, :

StaticExample::$aNum;

StaticExample self:

class StaticExample {`
    static public $aNum = 0;

    static public function sayHello() {
        self::$aNum++;
        print "hello (".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.

Not sure what was going on in your code.

0
source

Source: https://habr.com/ru/post/1770956/


All Articles