Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is better in PHP, Static Variable or Private Variable?

I noticed two ways in PHP to do the same thing. Can you tell me which way is a better programming practice?

In the first example, I use a private variable on the class. On the second example, I use a static variable in a class method.

class Test {
  private $_myvar;
  public function getVar(){
    if (!isset($this->_myvar)) {
      $this->_myvar = "test\n";
    }
    return $this->_myvar;
  }
}
$oTest = new Test();
echo $oTest->getVar(); // sets var first time and returns it
echo $oTest->getvar(); // pulls from cache

Or:

class Test {
  public function getVar(){
    static $myvar;
    if (!isset($myvar)) {
      $myvar = "test\n";
    }
    return $myvar;
  }
}
$oTest = new Test();
echo $oTest->getVar(); // sets var first time and returns it
echo $oTest->getvar(); // pulls from cache
like image 295
Volomike Avatar asked May 18 '11 23:05

Volomike


People also ask

Should static variables be public or private?

Static variables are created when the program starts and destroyed when the program stops. Visibility is similar to instance variables. However, most static variables are declared public since they must be available for users of the class. Default values are same as instance variables.

Is static variable faster?

Using static variables may make a function a tiny bit faster. However, this will cause problems if you ever want to make your program multi-threaded. Since static variables are shared between function invocations, invoking the function simultaneously in different threads will result in undefined behaviour.

Can static be private in PHP?

This is separate from the visibility of that variable - a public static property exists once per class, and can be accessed from everywhere; a private static property exists once per class, but can only be accessed from inside that class's definition.

Why static variable should not be used?

Static variables are generally considered bad because they represent global state and are therefore much more difficult to reason about. In particular, they break the assumptions of object-oriented programming.


2 Answers

That is like saying which room is better the Kitchen or the Bathroom, they are both rooms, but they have different functions.

A static variable is the same in multiple objects.

An instance variable, declared via private above is particular to a given object.

Note that private is an access modifier, static is not, a variable can be both.

In the location you have your static variable, within the function, it is not a class/object variable at all, but a traditional function-level static variable, which will be single-instanced across all calls to the function, making it similar to a class-level static variable, but only accessible within the method it is defined within.

like image 124
Orbling Avatar answered Oct 05 '22 01:10

Orbling


With the class property (be it public, private or protected), the variable is accessible to other parts of the class.

With the static variable, it is only visible to that method of the class.

I would suggest using the class property (but probably not private, which I generally don't find much use for; protected is normally a better idea) as it's easier for testing later; you can't do anything to unset, alter or check the static variable.

I see some possible confusion in the other answers here between static variables and static class properties. PHP uses the same modifier, but the behaviour is quite different; an example follows.

<?php
class Foo {
   // Static class property
   public static $bar = 'bar';

   public function blarg() {
      static $bar;
      if (empty($bar)) {
         $bar = 'blarg';
      }
      return $bar;
   }
}

In the above example the static class property can be accessed using Foo::$bar (or self::$bar within the class and static::$bar in PHP 5.3); the static variable cannot and is only visible inside the function blarg().

like image 24
El Yobo Avatar answered Oct 05 '22 01:10

El Yobo