gpt4 book ai didi

php static::inside 匿名函数

转载 作者:搜寻专家 更新时间:2023-10-31 21:04:50 24 4
gpt4 key购买 nike

代码在本地机器上运行良好,但在远程服务器上却很奇怪。远程服务器上的 PHP 5.5.9,本地机器上的 PHP 5.5.28远程服务器(PHP 版本、服务器配置等)出现这种奇怪行为的原因是什么?

class A {
...
public static f1(){
...
self::$f2 = static::f2();
...
}
...
protected static function f2() {
...
var_dump(static::class); // returns B on the local machine and on the remote server

$f3 = function () {
var_dump(static::class); // returns B on the local machine and returns A on the remote server
...
$data = static::f4() ...
...
};
...
}
...
}
class B extends A{...}
...
self::$B = B::f1();

最佳答案

这是一个 known bug已在 PHP 5.5.14 中修复.以下是原始错误报告的引述:

Closures do not correctly capture the late bound class (static::) in some cases

Description:

When a PHP closure is created, it is supposed to capture the late bound class of the enclosing function. There are a number of cases involving derived classes and static methods or static closures (or both) where it doesn't work correctly.

The test script I've included demonstrates the problem. I've tested it on PHP 5.6.0alpha1, but I can also reproduce the problem on various builds of PHP 5.5.

关于php static::inside 匿名函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34362789/

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