如何在静态方法中调用类的成员变量? [英] How can I call member variables of a class within a static method?

查看:608
本文介绍了如何在静态方法中调用类的成员变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一些方法来自动加载带有函数的帮助文件。现在我唯一的问题是如何调用该类中的变量。

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;
    }
}

我可以从任何地方调用函数 spl_autoload_register()

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

some_helperclass:: some_func();


推荐答案

您必须使用 self :: $ some_variable

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

成员变量必须也被声明为静态。

The member variable has to be declared static too.

这篇关于如何在静态方法中调用类的成员变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆