From cce6967acfcd5f6e509bed627b571ca260a10c1f Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 18 Jun 2026 10:52:53 +0800 Subject: [PATCH] =?UTF-8?q?fix(php):=20=E4=BF=AE=E5=A4=8D=E9=9D=99?= =?UTF-8?q?=E6=80=81=E5=B1=9E=E6=80=A7=E6=99=9A=E7=BB=91=E5=AE=9A=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 CompilerBase.php 中的静态属性处理逻辑,正确区分 self 和 static 关键字 - 为 native-property-full-name.php 添加 writeStatic 测试方法 - 更新 NativePropertyTest.php 中的编译方法返回值类型和测试用例 - 添加静态属性晚绑定功能的完整测试用例 static-prop-late-static-binding.phpt --- phpunit/code/native-property-full-name.php | 7 ++++ phpunit/src/NativePropertyTest.php | 16 +++++++- src/Php/CompilerBase.php | 5 ++- .../static-prop-late-static-binding.phpt | 39 +++++++++++++++++++ 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 tests/aot/static/static-prop-late-static-binding.phpt diff --git a/phpunit/code/native-property-full-name.php b/phpunit/code/native-property-full-name.php index af197a4a..e954ccbb 100644 --- a/phpunit/code/native-property-full-name.php +++ b/phpunit/code/native-property-full-name.php @@ -37,6 +37,12 @@ namespace NativePropSource\Target { return static::$count; } + public static function writeStatic(int $value): int + { + static::$count = $value; + return static::$count; + } + public static function readParent(): int { return parent::$count; @@ -64,6 +70,7 @@ namespace { var_dump(\NativePropSource\Target\readStaticByUse()); var_dump(\NativePropSource\Target\Child::readSelf()); var_dump(\NativePropSource\Target\Child::readStatic()); + var_dump(\NativePropSource\Target\Child::writeStatic(4)); var_dump(\NativePropSource\Target\Child::readParent()); } } diff --git a/phpunit/src/NativePropertyTest.php b/phpunit/src/NativePropertyTest.php index 6f32781e..b01b6b80 100644 --- a/phpunit/src/NativePropertyTest.php +++ b/phpunit/src/NativePropertyTest.php @@ -5,7 +5,7 @@ use PhpAot\Php\Exception\TestError; class NativePropertyTest extends \BaseTest { - private function compile(string $file): void + private function compile(string $file): string { global $translator; @@ -17,6 +17,7 @@ class NativePropertyTest extends \BaseTest $compiler->convertFile($testFile); $this->addToAssertionCount(1); + return ROOT_PATH . '/build/phpunit/code/' . basename($file, '.php') . '.cc'; } public function testFindNativePropertyUsesFullClassNameAcrossBranches(): void @@ -28,6 +29,19 @@ class NativePropertyTest extends \BaseTest } } + public function testStaticStaticPropertyUsesDynamicCalledClassPath(): void + { + try { + $outputFile = $this->compile('native-property-full-name.php'); + } catch (TestError $e) { + $this->fail($e->getMessage()); + } + + $code = file_get_contents($outputFile); + $this->assertStringContainsString('php::getStaticProperty(php_get_called_class(this_), "count")', $code); + $this->assertStringContainsString('php::getStaticProperty(php_get_called_class(this_), "count") = php::toInt(value)', $code); + } + public function testCannotAccessPrivateNativePropertyFromUnrelatedClass(): void { $this->exec('Cannot access private property `value` of class `NativePrivateOwner`', 'native-property-private-other-class.php'); diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 8755a759..3257b200 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -4723,7 +4723,10 @@ class CompilerBase extends \PhpAot\Core\Translator if ($this->isNameExpr($expr->class) and $this->isIdExpr($expr->name)) { $class = $this->parseIdentifier($expr->class); $propertyName = $this->parseIdentifier($expr->name); - if ($class === 'self' || $class === 'static') { + if ($class === 'static') { + return null; + } + if ($class === 'self') { if ($this->classDef->trait) { return Symbol::getStaticProperty() . '(' . Symbol::getCalledCe() . ', ' . $this->getLiteralString($propertyName) . ')'; } diff --git a/tests/aot/static/static-prop-late-static-binding.phpt b/tests/aot/static/static-prop-late-static-binding.phpt new file mode 100644 index 00000000..573bdfa3 --- /dev/null +++ b/tests/aot/static/static-prop-late-static-binding.phpt @@ -0,0 +1,39 @@ +--TEST-- +Static native properties use late static binding for static::$prop +--FILE-- + +--EXPECT-- +int(1) +int(2) +int(1) +int(9) +int(9)