静态成员可以使用非静态成员,反之亦然吗? [英] could static members use nonstatic members and vice versa?

查看:136
本文介绍了静态成员可以使用非静态成员,反之亦然吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在静态方法中使用非静态成员吗​​?



例如

  $ this-> nonStaticProperty 
$ this-> nonStaticMethod()

解决方案

http://php.net/manual/en/language.oop5.static.php p>


将类属性或方法声明为static使得它们可以访问而无需实例化类。声明为static的属性不能通过实例化的类对象访问(虽然可以使用静态方法)。


非静态成员在静态函数中,因为它们在函数的范围之外。但您可以在非静态函数中使用静态成员。


could i use nonstatic members inside a static method?

eg.

 $this->nonStaticProperty
 $this->nonStaticMethod()

and vice versa that is to say use static members inside non-static methods?

解决方案

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

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).

You can't use non-static members in a static function, since they're outside the function's scope. But you can use static members in a non-static function.

这篇关于静态成员可以使用非静态成员,反之亦然吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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