通过静态和非静态方法访问静态属性? [英] Access static property through static and non-static methods?

查看:156
本文介绍了通过静态和非静态方法访问静态属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,它有一些静态的,一些不是静态的方法.它具有静态属性.我试图在所有方法中访问该属性,但找不到正确的语法.

I have a class and it has some static, some not static methods. It has a static property. I'm trying to access that property inside all of it's methods, I can't figure out the right syntax.

我有什么?

class myClass {
    static public $mode = 'write';
    static public function getMode() {
        return myClass::$mode; 
    }
    public function getThisMode() {
        return $this->mode;
    }
}

有人可以告诉我该代码的实际语法吗?

Can anyone tell me the actual syntax for this one?

推荐答案

对于静态属性,即使在非静态函数中也要使用以下内容

For static properties use the following even inside a non static function

return self::$mode;

之所以这样,是因为无论对象是否已实例化,都存在静态属性.因此,我们只是使用相同的预先存在的属性.

The reason for this is because the static propery exists whether an object has been instantiated or not. Therefore we are just using that same pre-existing property.

这篇关于通过静态和非静态方法访问静态属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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