From 5bf12a10ed1945a7ced1966ec1666624a81119e7 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 22 Apr 2026 13:21:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(php):=20=E6=B7=BB=E5=8A=A0=E5=AF=B9=20trai?= =?UTF-8?q?t=20=E9=AD=94=E6=9C=AF=E5=B8=B8=E9=87=8F=20=5F=5FTRAIT=5F=5F=20?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 CompilerBase 中添加 Scalar_MagicConst_Trait 的解析支持 - 为 __TRAIT__ 常量实现作用域检查逻辑 - 为 __CLASS__ 常量添加 trait 环境下的返回逻辑 - 在 Preprocessor 中添加类属性钩子不支持的错误提示 - 添加 trait 魔术常量的功能测试用例 - 更新 trait 示例代码以展示魔术常量使用 --- examples/property-hook.php | 24 ++++++++++++++++++++++++ examples/trait.php | 9 +++++++-- src/Php/CompilerBase.php | 12 ++++++++++++ src/Php/Preprocessor.php | 3 +++ tests/aot/trait/magic-const.phpt | 28 ++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 examples/property-hook.php create mode 100644 tests/aot/trait/magic-const.phpt diff --git a/examples/property-hook.php b/examples/property-hook.php new file mode 100644 index 00000000..5eb5cbe8 --- /dev/null +++ b/examples/property-hook.php @@ -0,0 +1,24 @@ +modified) { + return $this->foo . ' (modified)'; + } + return $this->foo; + } + set(string $value) { + $this->foo = strtolower($value); + $this->modified = true; + } + } +} + +function main() +{ + $example = new Example(); + $example->foo = 'Hello World'; +} \ No newline at end of file diff --git a/examples/trait.php b/examples/trait.php index 9c7ee06c..2e8e4a81 100644 --- a/examples/trait.php +++ b/examples/trait.php @@ -3,6 +3,8 @@ trait TraitA { public function sayHello() { echo 'Hello'; + var_dump(__TRAIT__); + var_dump(__CLASS__); } } @@ -24,5 +26,8 @@ class MyHelloWorld } } -$obj = new MyHelloWorld(); -$obj->sayHelloWorld(); +function main() +{ + $obj = new MyHelloWorld(); + $obj->sayHelloWorld(); +} diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 3d7fe2ad..36e53958 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -478,6 +478,7 @@ class CompilerBase extends \PhpAot\Core\Translator case 'Scalar_MagicConst_Function': case 'Scalar_MagicConst_Method': case 'Scalar_MagicConst_Class': + case 'Scalar_MagicConst_Trait': return $this->parseMagicConst($expr); case 'Scalar_InterpolatedString': return $this->parseInterpolatedString($expr); @@ -3472,6 +3473,17 @@ class CompilerBase extends \PhpAot\Core\Translator case 'Scalar_MagicConst_Function': return '"' . $this->escapeString($function) . '"'; case 'Scalar_MagicConst_Class': + if (!$this->classDef) { + $this->fatalError($expr, 'The magic constant `__CLASS__` is not allowed in global scope'); + } + if ($this->classDef->trait) { + return Symbol::getCalledClass(); + } + return '"' . $this->escapeString($class) . '"'; + case 'Scalar_MagicConst_Trait': + if (!$this->classDef or !$this->classDef->trait) { + $this->fatalError($expr, 'The magic constant `__TRAIT__` is not allowed in global scope'); + } return '"' . $this->escapeString($class) . '"'; case 'Scalar_MagicConst_Method': return '"' . $this->escapeString($class) . '::' . $this->escapeString($this->method) . '"'; diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index bf6ae712..d7dd2351 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -546,6 +546,9 @@ class Preprocessor extends CompilerBase protected function parseClassPropertyDef(Node\Stmt\Property $v): void { + if ($v->hooks and count($v->hooks) > 0) { + $this->fatalError($v, 'The class property hooks are not supported'); + } $oriCtx = $this->context; $this->context = $this->classDef->propertyContext; $flags = $this->parseModifiers($v->flags); diff --git a/tests/aot/trait/magic-const.phpt b/tests/aot/trait/magic-const.phpt new file mode 100644 index 00000000..9e306a90 --- /dev/null +++ b/tests/aot/trait/magic-const.phpt @@ -0,0 +1,28 @@ +--TEST-- +trait full class name +--FILE-- +sayHello(); + } +} + +function main() +{ + $obj = new MyHelloWorld(); + $obj->sayHelloWorld(); +} +?> +--EXPECT-- +string(6) "TraitA" +string(12) "MyHelloWorld" \ No newline at end of file