English 中文(简体)
为什么静态成员在购买力平价中当值的物体数时继承价值?
原标题:Why does Static member variable inherits the value when instantiatiing object number of times in PHP?

i 正在测试关于如何准确开展工作的《法定关键词》,而在此之前,人们不了解正在发生的情况。

审议两个类别:<代码>ClassNameA &ClassNameB和以下代码。

<>ClassName 无法定关键词

class ClassNameA
{
    private $name;

    public function __construct($value) {
        if($value !=   ) {
            $this->name = $value;
        }
        $this->getValue();
    }
    public function getValue() {
        echo $this->name;
    }
}

<>ClassNameB with Static Keyword

class ClassNameB
{
    private static $name;

    public function __construct($value) {
        if($value !=   ) {
            self::$name = $value;
        }
        $this->getValue();
    }
    public function getValue() {
        echo self::$name;
    }
} 

多次使用NameA类

$a = new ClassNameA(12);
echo  <br/> ;
$a = new ClassNameA(23);
echo  <br/> ;
$a = new ClassNameA(  ); //Argument given is Empty here

它的产出是:

12
23

现在使用NameB级的多次瞬时发射物体

$a = new ClassNameB(12);
echo  <br/> ;
$a = new ClassNameB(23);
echo  <br/> ;
$a = new ClassNameB(  ); //Argument given is Empty here

产出如下:

12
23
23

Note the extra value 23 it is taking even if the Argument Passed is Empty. Is this a bug? or am i missing something?

最佳回答

这就是<代码>static财产的性质。 静态财产是非物财产的一类财产。

当你通过空白时,根据条件,静态财产的价值将得不到更新,最后价值仍存在于静态财产中。

由于静态财产不受任何物体的约束,因此可以不需要任何物体。

$a = new ClassNameB(12); //static property is set to 12
echo  <br/> ;
$a = new ClassNameB(23); //static property is update to 23
echo  <br/> ;
$a = new ClassNameB(  ); //static property is not updated here it is still 23

EDIT

You can understand like this:-

if($value !=   ) {
    $this->name = $value; //
 }

以上准则正在确定目前物体的财产价值(现在开始确定的目标)。

因此,你写了字

$a = new ClassNameA(12);

正在制定<代码> 姓名: 财产至12,用于标的<代码>a;

$a = new ClassNameA(23);

正在制定<代码> 姓名: 财产至<编码> 23,用于标的<代码>a;

但当财产为<编码>static时。 也就是说,不是针对任何物体的整个类别。

页: 1

if($value !=   ) {
    self::$name = $value;
 }

上述法典规定了固定财产价值。 请注意,您在此撰写了<代码>自行,而不是$>,使该代码仅用于这一类别,而不适用于任何物体。

我试图更好地解释它,但不知道它是如何向你们解释的。

问题回答

A static member is a single instance across the entire application, not once per object. For example

class Example {
  static public $var;

  static public MyFunction() {
    echo "MyFunction
";
  }
}

Example::$var = 123;
echo Example::$var;
Example::MyFunction();

请注意,我们不需要树立一个“实例”的例子,主要是它把变数换到类别。 这样做无效:

$example = new Example();
echo $example->var;
$example->MyFunction();

您也可以在课堂内提及该课。

self::$var
self::MyFunction();

如果你需要的话,可以安全地在以后重新命名。 固定功能不能通过固定成员或方法进入。





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...