开发者

could static members use nonstatic members and vice versa?

开发者 https://www.devze.com 2022-12-19 00:03 出处:网络
could i use nonstatic members inside a static method? eg. $this->nonStaticProperty $this->nonStaticMe开发者_JAVA百科thod()

could i use nonstatic members inside a static method?

eg.

 $this->nonStaticProperty
 $this->nonStaticMe开发者_JAVA百科thod()

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.


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


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


class foo {
    
      
    
    public static function bar()
    {
        echo "bar calling\n";
    }
    
}



$ff = new foo();
echo $ff->bar();

echo foo::bar();

Check here : http://sandbox.onlinephpfunctions.com/code/48b9540c8d897f46dcd8ea13595950fa78fa96e5

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号