php php静态属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14930046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:13:37  来源:igfitidea点击:

php static property

phpclassstatic

提问by nut

I have two code snippets (regarding the statickeyword) which I expect them to have same output, but actually the outputs are different.

我有两个代码片段(关于static关键字),我希望它们具有相同的输出,但实际上输出是不同的。

The question is why?

问题是为什么?

Snippet 1

片段 1

class base
{
    public static $var = 1;
}

class sub extends base
{
    public static $var = 2;
}

echo base::$var; // Outputs 1

1

1

Snippet 2

片段 2

class base2
{
    public static $var2 = 1;
}

class sub2 extends base2
{
}

sub2::$var2 = 2;
echo base2::$var2; // Outputs 2

2

2

回答by Vinod Jayachandran

I would like to add one more point to it.

我想再补充一点。

STATIC variable are notassociated to any particular instance/object of a class. Hence you modify the variable with Parent Class reference or Child Class reference, the same copy gets modified.

静态变量是相关联的一类的任何特定实例/对象。因此,您使用父类引用或子类引用修改变量,相同的副本被修改。

Hence apart from understanding Public Static as Global, Please understand it as not associated to any particular instance, hence with any class hierarchy reference you update a static variable , same memory location gets updated.

因此,除了将公共静态理解为全局之外,请将其理解为与任何特定实例无关,因此对于任何类层次结构引用,您更新静态变量,相同的内存位置都会更新。

--

——

Vinod

维诺德

回答by Akash KC

Public static variableacts like globalvariable.

Public static variable就像global变量一样。

As the scope of public static variablelies globally, you can ensure that any changes to the global variablecan be seen across the program.This concept, you can analyze in your sample program....

由于范围public static variable是全局的,您可以确保global variable在整个程序中都可以看到对 的任何更改。这个概念,您可以在您的示例程序中分析....

Please have look in Static variable inheritance.

请查看静态变量继承

回答by zamil

Since the sub2 class does not have var2 it takes the value of the base2 class.So when u assigned a new value using this sub2::$var2 = 2;it changed to new value.

由于 sub2 类没有 var2,它采用 base2 类的值。因此,当您使用sub2::$var2 = 2;它分配新值时,它更改为新值。

In first code you have overridden that in your extended class.Both variable have scope inside their respective classes.

在第一个代码中,您在扩展类中覆盖了它。两个变量在它们各自的类中都有作用域。

回答by Adam

If you pass a static variable to a subclass, then this variable is shared (it always has the same value in both classes). If you overwrite the static variable in a subclass, then its a new static variable independent of the static variable from the parent.

如果将静态变量传递给子类,则该变量是共享的(它在两个类中始终具有相同的值)。如果覆盖子类中的静态变量,则它是一个独立于父类静态变量的新静态变量。

In Snippet 1base::varand sub::varhave different memory allocations, because you defined $var = 2;in the class sub.

Snippet 1 中base::varsub::var有不同的内存分配,因为您$var = 2;在类中定义了sub.

In Snippet 2base::varand sub::varhave the same memory allocation, because you did not specify $var2in the class sub2.

片段2base::varsub::var具有相同的内存分配,因为你没有指定$var2的类sub2

Thats why base:varis not changing. It would also not change if you change $varfrom subclass later like this:

这就是为什么base:var没有改变。如果你以后像这样$varsub课堂上改变,它也不会改变:

class base
{
    public static $var = 1;
}

class sub extends base
{
    public static $var = 2;
}
sub::var = 3
echo base::var; // Outputs 1