fix(php): 解决类方法参数中使用$this的问题并优化静态属性访问

- 添加对类方法中使用$this作为参数名的错误检查
- 修复静态属性被当作动态属性访问时返回null的逻辑
- 在关键词列表中添加this_作为phpx保留关键字
pull/1/head
韩天峰 5 months ago
parent fa6105ebdf
commit 59698b5319
  1. 17
      src/Php/CompilerBase.php
  2. 1
      src/Php/Constants.php

@ -1046,14 +1046,17 @@ class CompilerBase extends \PhpAot\Core\Translator
$this->fatalError($param, 'Variadic parameters cannot be passed by reference'); $this->fatalError($param, 'Variadic parameters cannot be passed by reference');
} }
} }
$name = $this->parseIdentifier($param->var); $name = $this->parseIdentifier($param->var);
$type = $this->parseParameterType($param, $name); if ($this->method and $name == 'this_') {
$this->fatalError($param, 'Cannot use `$this` as parameter of class method');
}
$type = $this->parseParameterType($param, $name);
if ($param->variadic) { if ($param->variadic) {
$list[] = self::TYPE_ARRAY . ' ' . $name; $list[] = self::TYPE_ARRAY . ' ' . $name;
} else { } else {
$list[] = $type . ' ' . $name; $list[] = $type . ' ' . $name;
} }
$argInfo = new ArgInfo(); $argInfo = new ArgInfo();
$argInfo->name = $name; $argInfo->name = $name;
$argInfo->type = $type; $argInfo->type = $type;
$argInfo->byRef = $param->byRef; $argInfo->byRef = $param->byRef;
@ -4005,6 +4008,10 @@ class CompilerBase extends \PhpAot\Core\Translator
$classDef = $this->getClass($findClass); $classDef = $this->getClass($findClass);
if ($classDef->hasProperty($property)) { if ($classDef->hasProperty($property)) {
$propertyDef = $classDef->getProperty($property); $propertyDef = $classDef->getProperty($property);
// 属性是静态的,但作为动态属性访问,只能动态获取
if ($propertyDef->isStatic()) {
return null;
}
if ($propertyDef->isPublic()) { if ($propertyDef->isPublic()) {
return $this->getPropertyOffset($classDef->getNamespacedName(false), $property); return $this->getPropertyOffset($classDef->getNamespacedName(false), $property);
} }

@ -54,6 +54,7 @@ class Constants
'template', 'template',
'namespace', 'namespace',
'errno', // Linux error code 'errno', // Linux error code
'this_', // phpx keywords
]; ];
public const UNSUPPORTED_FUNCTIONS = [ public const UNSUPPORTED_FUNCTIONS = [

Loading…
Cancel
Save