Access Static properties using PHP object

后端 未结 3 569
粉色の甜心
粉色の甜心 2021-02-07 01:57

This is with reference to Get a static property of an instance, I am a newbie and have the following code :

class Foo
{
   public static $my_static = 1;
}

class         


        
相关标签:
3条回答
  • 2021-02-07 02:09

    Static properties may be accessed on various ways.

    Class::$aStaticProp; //by class name
    
    $classname::$aStaticProp; // As of PHP 5.3.0 by object instance
    

    Static properties cannot be accessed through the object using the arrow operator ->.

    As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static).

    More you can read in manual

    0 讨论(0)
  • 2021-02-07 02:22

    within the class you have to use like self::$staticPropery if the function accessing to the variable is also static.

    0 讨论(0)
  • 2021-02-07 02:26

    $instance::$staticProperty is simply a convenience shorthand for Class::$staticProperty. Since you already have an instance of a class and the syntax is unambiguous, PHP saves you from writing a potentially long class name. There's no functional difference.

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