fix(compiler): 修复命名空间内类名解析及use别名查找

pull/2/head
Yurun 2 months ago
parent 19164e92bc
commit f2e063b41d
  1. 11
      src/Php/CompilerBase.php
  2. 52
      tests/aot/namespace/ns-same-as-class.phpt

@ -4732,8 +4732,10 @@ class CompilerBase extends \PhpAot\Core\Translator
$this->fatalError($expr, 'Cannot access parent:: when current class does not extend any class');
}
$class = $this->classDef->extends;
} else {
$class = $this->getNamespacedClassName($class);
}
$nativeProperty = $this->findNativeProperty($expr, $propertyName, $class, $this->namespace, true);
$nativeProperty = $this->findNativeProperty($expr, $propertyName, $class, true);
if ($nativeProperty) {
$expr->setAttribute('nativeProperty', $nativeProperty);
return $nativeProperty;
@ -4745,13 +4747,10 @@ class CompilerBase extends \PhpAot\Core\Translator
/**
* @param NodeAbstract $expr 仅用于输出错误日志
*/
protected function findNativeProperty(NodeAbstract $expr, string $property, string $class, string $namespace = '', bool $static = false): ?string
protected function findNativeProperty(NodeAbstract $expr, string $property, string $class, bool $static = false): ?string
{
$findClass = $class;
if ($namespace) {
$findClass = $namespace . '\\' . $class;
}
$scope = $this->class ? ltrim($namespace . '\\' . $class, '\\') : '';
$scope = $this->class ? $class : '';
$propertyDef = null;
$classDef = null;
while (true) {

@ -0,0 +1,52 @@
--TEST--
use class A\B (without alias) inside namespace A\B, call B::$v
--FILE--
<?php
namespace A {
class B
{
public static $v = 123;
}
}
namespace A\B {
use A\B;
function testProp(): int {
return B::$v;
}
class C extends B {
public static function testSelf(): int
{
return self::$v;
}
public static function testStatic(): int
{
return static::$v;
}
public static function testParent(): int
{
return parent::$v;
}
}
}
namespace {
function main() {
var_dump(\A\B\testProp());
var_dump(\A\B\C::testSelf());
var_dump(\A\B\C::testStatic());
var_dump(\A\B\C::testParent());
}
}
?>
--EXPECT--
int(123)
int(123)
int(123)
int(123)
Loading…
Cancel
Save