Just checking something out ...

If I declare a variable inside a class definition as ...

private static $items = array();

then $items is a property of the class, not any particular instance. There can and will only be one array named $items.

On the other hand, if I declare the variable inside the class definition as ...

private $items = array();

then $items will be a property of each new instance of the class, and I will have as many $items arrays as I have instances.

Am I understanding this correctly? I need to make very sure before I dive into something.

    yeah, static properties cannot be accessed with the -> operator. Read the OOP Static section of the Php manual for a extended discussion of the subject.

      I understood about self:: versus $this before, but what I never grasped till yesterday (and that is if I did grasp it right), is that if you have an array as a property of a Class,

      private $items

      every instance has its own array property with that name. If you have 20 different instances, you potentially have 20 different arrays containing 20 potentially different sets of data.

      P.S. My amazement is probably more understandable if you realize (1) I'm new to OOP, and (2) most of my development so far has been creating Singletons (ergo, one array) per Class to set up the environment for my application. Now that I'm beginning to use Classes with multiple instances, it dawned on me each instance can have its own totally unique array.

        Am I understanding this correctly? I need to make very sure before I dive into something.

        You've got it right there. And since you've marked them private, they're only visible to that exact class (in the static case) or that particular object (in the instance case).

        <?php
        class foo
        {
        	private static $bong;
        
        function make_bong($v)
        {
        	self::$bong = $v;
        }
        
        function bing_bong()
        {
        	echo self::$bong;
        }
        
        }
        
        
        $foo = new foo;
        $bar = new foo;
        
        $foo->make_bong(42);
        $bar->bing_bong();
        
        echo foo::$bong;
        
          Write a Reply...