Are class variables possible in PHP4?

I'm aware that functions can have static variables, using the 'static' keyword and that methods can be accessed as Class methods, using '::'. But I can't work out how to create a static class variable.

So far, my only options are to use a global variable, or to keep the variable as a static variable of a class method.

    How's this:

    http://www.php.net/manual/en/keyword.paamayim-nekudotayim.php
    From user comments on the manual:
    That's how to make class' static variable in PHP4. That means reference returned by function value() is the same for all instances of class O, thus they can share common data. This reference is common just for instances on same PHP page, of course.

    <?  class O { 
      function &value() { 
        static $val=0; 
        return $val; 
      } 
     } 
    
     $o1= &new O; 
     $o2= &new O; 
    
    echo "\$o1: ".$o1->value()."<b"."r>"; 
     echo "\$o2: ".$o2->value()."<b"."r>"; 
    
    $ref1= &$o1->value(); 
     $ref1= 5; 
    
     $ref2= &$o2->value(); 
     $ref2= 10; 
    
     echo "\$o1: ".$o1->value()."<b"."r>"; 
     echo "\$o2: ".$o2->value()."<b"."r>"; 
    ?>
    

      Thanks. I'm pretty new to this, if you didn't guess.

      Is there any way to do it without creating an instance?

      Here's where I got to, using a getter and setter method, but I'd like to do it with a varaible:

      <?
      class A{
      	function getVar($val=NULL){
      		static $class_variable = 0;
      		if($val != NULL){
      			$class_variable = $val;
      		}
      		return $class_variable;
      	}
      	function setVar($val=0){
      		return A::getVar($val);
      	}
      }
      
      A::setVar(3);
      print A::getVar();
      
      ?>
      

        Just a few questions about your use of '&'. I'm familiar with using it to pass references around, but you have used it in a couple of ways that I haven't seen.

        function &value() {
        }
        

        I'm guessing the '&' makes each instance use a reference to the same function, so that the static vars in each instance also reference the same var.... right?

        $o1= &new O;
        

        I haven't seen '&new' used before. What does it do? And is this necessary for your code to work?

          the code I posted is actually from user contributed notes on the manual at www.php.net

          That's why I included the link.

          Actually I'm not sure what function &value() does.

          I have seen &new before, in the GTK-PHP stuff.

          There's only one way to find out if it's necessary.

          My guess is that it's necessary because you are not instantiating any objects, but you are returning values from them. Therefore it's necessary to keep the memory associated with the "non-existant" return values.
          But I could be totally wrong.

          PHP's OOP functionality is not very good.
          I dont' think you can do it with a variable, which is why you have to use the set and get functions.

            Thanks anyway. The link was pretty useful - I have been trying to find that exact page, but searching for things like "::" don't turn anything up...

              Write a Reply...