diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 66e09eed..5caa92e4 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -4271,8 +4271,13 @@ class CompilerBase extends \PhpAot\Core\Translator $class = $this->parseIdentifier($expr->class); $self = false; if ($class === 'self' or $class === 'this_') { - $self = true; - $class = $this->class; + // Trait 读取常量,必须动态获取类名 + if ($this->classDef->trait) { + $class = 'static'; + } else { + $self = true; + $class = $this->class; + } } $const = $this->escapeString($this->parseIdentifier($expr->name)); diff --git a/src/Php/Translator.php b/src/Php/Translator.php index d83f220d..0272797f 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -548,6 +548,7 @@ zend_module_entry {$moduleName}_module_entry = { STANDARD_MODULE_PROPERTIES, }; CODE; + $code .= PHP_EOL . PHP_EOL; if ($this->buildMode === 'ext') { $code .= "ZEND_GET_MODULE({$moduleName});\n"; diff --git a/tests/aot/trait/008.phpt b/tests/aot/trait/008.phpt new file mode 100644 index 00000000..e072afb5 --- /dev/null +++ b/tests/aot/trait/008.phpt @@ -0,0 +1,34 @@ +--TEST-- +Method conflict in traits +--FILE-- +prop); + var_dump(self::HELLO); + var_dump(self::$staticProp); + } +} + +class TraitsTest +{ + use THello1; + protected string $prop = 'Hello 1'; + const HELLO = 'Hello 2'; + static int $staticProp = 2025; +} + +function main() +{ + $o = new TraitsTest; + $o->hello(); +} +?> +--EXPECT-- +string(10) "TraitsTest" +string(7) "Hello 1" +string(7) "Hello 2" +int(2025) \ No newline at end of file