简体   繁体   English

在其他语言中,PHP等价于静态变量是什么?

[英]What's the PHP equivalent of a static variable in other languages?

I'm wondering if PHP has a type of variable in classes that functions like static in other languages. 我想知道PHP是否在类中具有类型的变量,其功能类似于其他语言中的静态。 And by that I mean all objects of the same class use the same variable and when it's updated on one it's updated on every one. 我的意思是,同一个类的所有对象都使用相同的变量,当它更新时,每个对象都会更新。 Static is close because it is shared throughout all objects but I need to be able to update it. 静态是接近的,因为它在所有对象中共享,但我需要能够更新它。 Will I have to use globals for this? 我是否必须使用全局变量?

The correct answer is that there is no equivalent in PHP to final, but static seems like what you wanted in the first place anyway. 正确的答案是PHP中没有与final相当的东西,但静态看起来就像你想要的那样。

static has the property that it will have the same value across all instances of a class, because it is not tied to a particular instance. static具有在类的所有实例中具有相同值的属性,因为它不依赖于特定实例。

You will need to use the :: operator to access it, because being static, you cannot use -> . 您将需要使用::运算符来访问它,因为它是静态的,您不能使用- >

I think static is what you want. 我认为静电就是你想要的。 You can update a static variable, you just have to do it in a "static context" (ie. using the :: operator. 您可以更新静态变量,只需在“静态上下文”中(即使用::运算符)。

class Class1 {
    protected static $_count = 0;

    public function incrementCount() {
        return self::$_count++;
    }
}

$instance1 = new Class1();
$instance2 = new Class1();
var_dump($instance1->incrementCount(), $instance2->incrementCount());

will output: 将输出:

int 0 int 0

int 1 int 1

You can update static properties: 可以更新静态属性:

class A {
   protected static $_foo = 0;

   public function increment()
   {
       self::$_foo++;
   }   

   public function getFoo()
   {
       return self::$_foo;
   }
}


$instanceOne = new A();
$instanceTwo = new A();


$instanceOne->getFoo(); // returns 0

$instanceTwo->increment();

$instanceOne->getFoo(); // returns 1

You can simply create variables in a PHP file say named Constants. 您可以在PHP文件中创建名为Constants的变量。

--Constants.php-- $DATABASE_NAME = "mysql" --Constants.php-- $ DATABASE_NAME =“mysql”

and include the file in your file. 并将文件包含在您的文件中。 You can change its value. 您可以更改其值。 It comes close to what you want, but it is not good call them constants because constants aren't meant to be changed, that's what confused me :). 它接近你想要的东西,但是不要把它们称为常量,因为常量并不意味着要改变,这就是困扰我:)。

I don't see why making the variable static doesn't work for what you described (but it has nothing to do with the keyword final)? 我不明白为什么使变量static不适合你所描述的(但它与关键字final无关)?

<?php

class Bla
{
    public static $var;

    public function __construct()
    {
        Bla::$var = Bla::$var + 1;
    }
}

$test = new Bla();
echo Bla::$var; // 1
$test = new Bla();
echo Bla::$var; // 2

?>

I think static is the keyword you are looking for. 我认为static 您正在寻找的关键字。

And there is nothing that prevents a static property from beeing "updated", in PHP : it is initialized the first time it's set, it keeps it value during the execution of the PHP script, but you definitly can set it to a new value. 并且没有什么可以防止static属性在PHP中被“更新”:它在第一次设置时被初始化,它在执行PHP脚本期间保持它的值,但你肯定可以将它设置为新值。

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

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