fix(aot): 修复AOT编译中类继承和常量定义相关问题

- 在WorkerA类中添加baz方法用于测试保护方法访问
- 修正isInheritedFrom方法中类名比较逻辑,确保正确的继承检查
- 更新对象实例类名获取方式,使用getFullClassName替代直接访问
- 修复常量定义解析中的多余换行符问题
- 扩展测试用例验证保护方法在不同实例间的调用行为
pull/1/head
韩天峰 4 months ago
parent 492a1dbd39
commit 84b9340998
  1. 4
      src/Php/CompilerBase.php
  2. 2
      src/Php/Translator.php
  3. 12
      tests/aot/ref/007.phpt

@ -4599,7 +4599,7 @@ class CompilerBase extends \PhpAot\Core\Translator
}
// 保护方法,只能当前类和子类使用
if ($flags & Modifiers::PROTECTED) {
return $this->isInheritedFrom($this->classDef->getNamespacedName(), $classDef->getNamespacedName());
return $this->isInheritedFrom($this->classDef->getNamespacedName(false), $classDef->getNamespacedName(false));
}
// 类外部调用,只允许调用 public 方法
return true;
@ -4615,7 +4615,7 @@ class CompilerBase extends \PhpAot\Core\Translator
{
$classDef = null;
if ($object === 'this_') {
$class = $this->class;
$class = $this->getFullClassName();
$classDef = $this->classDef;
} elseif (isset($this->context->objects[$object])) {
$class = $this->context->objects[$object];

@ -980,7 +980,7 @@ class Translator extends Preprocessor
$code .= $this->parseClass($v2);
break;
case 'Stmt_Const':
$this->parseConstDef($v2) . PHP_EOL;
$this->parseConstDef($v2);
break;
case 'Stmt_Function':
$code .= $this->parseFunction($v2) . PHP_EOL;

@ -7,6 +7,12 @@ class WorkerA
protected function foo(string $name, string &$value) {
$value = 'hello ' . $name;
}
public function baz(string $name) {
$value = '';
$this->foo($name, $value);
return $value;
}
}
class WorkerB extends WorkerA
@ -22,7 +28,11 @@ function main()
{
$o = new WorkerB;
var_dump($o->bar('php'));
$o2 = new WorkerA;
var_dump($o2->baz('swoole'));
}
?>
--EXPECT--
string(9) "hello php"
string(9) "hello php"
string(12) "hello swoole"
Loading…
Cancel
Save