简体   繁体   English

哪个在PHP,静态变量或私有变量中更好?

[英]Which is better in PHP, Static Variable or Private Variable?

I noticed two ways in PHP to do the same thing. 我注意到PHP中有两种方法可以做同样的事情。 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

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. static变量在多个对象中是相同的。

An instance variable, declared via private above is particular to a given object. 通过private声明的实例变量特定于给定对象。

Note that private is an access modifier, static is not, a variable can be both. 请注意, private是一个访问修饰符, static不是,变量可以是两者。

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. 在您拥有static变量的位置,在函数内,它根本不是类/对象变量,而是传统的函数级static变量,它将在函数的所有调用中单实例化,使其类似于类级static变量,但只能在其定义的方法中访问。

With the class property (be it public, private or protected), the variable is accessible to other parts of the class. 使用类属性(可以是public,private或protected),该类的其他部分可以访问该变量。

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; 我建议使用class属性(但可能不是私有的,我通常没有太多用处;受保护通常是一个更好的主意),因为以后测试更容易; 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; PHP使用相同的修饰符,但行为完全不同; 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); 在上面的例子中,可以使用Foo::$bar (或类中的self::$bar和PHP 5.3中的static::$bar )访问静态类属性; the static variable cannot and is only visible inside the function blarg() . 静态变量不能并且仅在函数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. 但是设置一个实例,第二个实例将看到相同的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM