could static members use nonstatic members and vice versa?

前端 未结 3 1870
南旧
南旧 2020-12-12 02:37

could i use nonstatic members inside a static method?

eg.

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

and vice versa

相关标签:
3条回答
  • 2020-12-12 03:09

    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.

    0 讨论(0)
  • 2020-12-12 03:17

    Not really, as you can't use $this in a static context.

    0 讨论(0)
  • 2020-12-12 03:26

    As a static member doesn't have an instance, it can't call instance methods (unless you create an instance inside that method).

    0 讨论(0)
提交回复
热议问题