How can I call member variables of a class within a static method?

笑着哭i 提交于 2020-01-02 01:55:47

问题


I am using some method to autoload helper files with functions. The only problem I am having now, is how to call the variables in that class.

Because I am not instantiating it as an object, $this won't work. But what will?

class some_helperclass {

var $some_variable  = '007';

public static function some_func()
    {
    //return 'all ok';
    if (self::some_variable !== FALSE)  
    {
       return  self::ip_adres;
    }
}

I can call the function from anywhere now with the help of spl_autoload_register().

some_helperclass:: some_func();

回答1:


You have to use self::$some_variable. Put the $ in there.

http://www.php.net/manual/en/language.oop5.static.php

The member variable has to be declared static too.




回答2:


Declare the variable as static too.

private static $some_variable;


来源:https://stackoverflow.com/questions/1750837/how-can-i-call-member-variables-of-a-class-within-a-static-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!