开发者

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

开发者 https://www.devze.com 2023-03-06 17:54 出处:网络
I noticed two ways in PHP to do the same thing. Can you tell me which way is a better programming practice?

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 retu开发者_运维问答rns 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


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.


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().


Neither is "better". It would be like asking whether a screwdriver is better than a tenon saw.

The private variable in your first example will only be available to that instance of the class (although only to its methods). Create a new instance, and the variable can and will have a different value.

The static variable in your second example will be available to all instances of that class, admittedly only in that method. But set it one instance, and a second instance will see the same value.

0

精彩评论

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

关注公众号