gpt4 book ai didi

php - 新 self 与新静态

转载 作者:IT老高 更新时间:2023-10-28 11:36:19 25 4
gpt4 key购买 nike

我正在转换 PHP 5.3 库以在 PHP 5.2 上运行。阻碍我的主要事情是使用后期静态绑定(bind),如 return new static($options); ,如果我将其转换为 return new self($options)我会得到相同的结果吗?

new selfnew static有什么区别?

最佳答案

will I get the same results?

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

What is the difference between new self and new static?

self 指的是实际写入 new 关键字的类。

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

在以下示例中,BA 继承了这两个方法。 self 调用绑定(bind)到 A 因为它是在 A 的第一个方法的实现中定义的,而 static绑定(bind)到被调用的类(另见 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

关于php - 新 self 与新静态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5197300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com