fix(trait): 解决trait中self关键字引用问题

- 在trait中使用self时动态获取类名而不是固定为当前类
- 添加对trait定义的检测逻辑
- 确保trait中的常量访问正确解析
- 修复了method conflict测试用例
- 添加了traits测试文件验证功能正确性
pull/1/head
韩天峰 4 months ago
parent d8a828558d
commit 5b66ef2161
  1. 9
      src/Php/CompilerBase.php
  2. 1
      src/Php/Translator.php
  3. 34
      tests/aot/trait/008.phpt

@ -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));

@ -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";

@ -0,0 +1,34 @@
--TEST--
Method conflict in traits
--FILE--
<?php
trait THello1
{
public function hello()
{
var_dump(self::class);
var_dump($this->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)
Loading…
Cancel
Save