开发者

static properties and instances

What will happen if I create a class with a static property and create two instances of it?

Will the static property be shared between both instances an开发者_如何学运维d not be duplicated?


Yes, that is the definition of a static property.

Static properties belong to the class, not instances of the class.

class SomeClass {
   private static $instanceCount = 0;

   function __construct() {
      self::$instanceCount++;
      //do other stuff.
   }

   function instanceCount() { 
      return self::$instanceCount;
   }

}

$one = new SomeClass();
echo $one->instanceCount(); //1    

$two = new SomeClass();

echo $one->instanceCount(); //2
echo $two->instanceCount(); //2
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜