php 新自我与新静态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5197300/
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 20:35:32  来源:igfitidea点击:

New self vs. new static

phpstaticlate-static-binding

提问by Mike

I am converting a PHP 5.3 library to work on PHP 5.2. The main thing standing in my way is the use of late static binding like return new static($options);, if I convert this to return new self($options)will I get the same results?

我正在转换 PHP 5.3 库以在 PHP 5.2 上工作。阻碍我前进的主要因素是使用像 那样的后期静态绑定return new static($options);,如果我将其转换为,我return new self($options)会得到相同的结果吗?

What is the difference between new selfand new static?

new self和 和有new static什么区别?

回答by BoltClock

will I get the same results?

我会得到相同的结果吗?

Not really. I don't know of a workaround for PHP 5.2, though.

并不真地。不过,我不知道 PHP 5.2 的解决方法。

What is the difference between new selfand new static?

new self和 和有new static什么区别?

selfrefers to the same class in which the newkeyword is actually written.

selfnew实际写入关键字的同一类。

static, in PHP 5.3's late static bindings, refers to whatever class in the hierarchy you called the method on.

static,在 PHP 5.3 的后期静态绑定中,指的是层次结构中调用该方法的任何类。

In the following example, Binherits both methods from A. The selfinvocation is bound to Abecause it's defined in A's implementation of the first method, whereas staticis bound to the called class (also see get_called_class()).

在以下示例中,BA. 该self调用必然会A因为它在定义A的实现第一种方法的,而static必然要调用的类(见get_called_class())。

class A {
    public static function get_self() {
        return new self();
    }

    public static function get_static() {
        return new static();
    }
}

class B extends A {}

echo get_class(B::get_self());  // A
echo get_class(B::get_static()); // B
echo get_class(A::get_self()); // A
echo get_class(A::get_static()); // A

回答by Marius Bal?ytis

If the method of this code is not static, you can get a work-around in 5.2 by using get_class($this).

如果此代码的方法不是静态的,您可以在 5.2 中使用get_class($this).

class A {
    public function create1() {
        $class = get_class($this);
        return new $class();
    }
    public function create2() {
        return new static();
    }
}

class B extends A {

}

$b = new B();
var_dump(get_class($b->create1()), get_class($b->create2()));

The results:

结果:

string(1) "B"
string(1) "B"

回答by Rain

In addition to others' answers :

除了其他人的答案:

static:: will be computed using runtime information.

static:: 将使用运行时信息计算。

That means you can't use static::in a class property because properties values :

这意味着您不能static::在类属性中使用,因为属性值:

Must be able to be evaluated at compile time and must not depend on run-time information.

必须能够在编译时进行评估,并且不得依赖于运行时信息。

class Foo {
    public $name = static::class;

}

$Foo = new Foo;
echo $Foo->name; // Fatal error

Using self::

使用 self::

class Foo {
    public $name = self::class;

}
$Foo = new Foo;
echo $Foo->name; // Foo

Please note that the Fatal error comment in the code i made doesn't indicate where the error happened, the error happened earlier before the object was instantiated as @Grapestain mentioned in the comments

请注意,我所做的代码中的致命错误注释并没有表明错误发生的位置,错误发生在将对象实例化为注释中提到的@Grapestain 之前