fix(php): 修复原生函数调用异常处理和静态方法调用解析

- 在原生函数调用周围添加 try-catch 块以处理 PlaceHolder 异常
- 添加对名称表达式类型的检查以正确解析静态方法调用
- 为动态类名静态方法调用实现字符串连接逻辑
- 添加静态方法调用功能的测试用例
pull/1/head
韩天峰 6 months ago
parent fd48162ab6
commit 1d6681ade3
  1. 12
      src/Php/CompilerBase.php
  2. 27
      tests/aot/static-method-001.phpt

@ -2207,7 +2207,11 @@ class CompilerBase extends \PhpAot\Core\Translator
$nativeFn = $this->findNativeFunction($name);
if ($nativeFn) {
$expr->setAttribute('nativeCall', $nativeFn);
return self::PREFIX . $nativeFn . '(' . $this->parseNativeCallArgs($expr->args, $nativeFn) . ')';
try {
return self::PREFIX . $nativeFn . '(' . $this->parseNativeCallArgs($expr->args, $nativeFn) . ')';
} catch (PlaceHolder) {
return $this->genPlaceHolder($this->identifierToStr($expr->name));
}
}
$code = $this->parseFuncCallWithOptimizer($name, $expr);
if ($code) {
@ -3612,7 +3616,7 @@ class CompilerBase extends \PhpAot\Core\Translator
$fn = 'php_get_called_ce(this_), php::getMethod(php_get_called_ce(this_), ' . $methodPtr . ')';
$this->beforeStmtLines[] = '// Static Method Call: static::' . $this->parseIdentifier($expr->name) . '()';
$placeHolder = $this->genArray(['php_get_called_class(this_)', $methodPtr]);
} else {
} elseif ($this->isNameExpr($expr->class)) {
if ($class === 'self') {
$class = $this->class;
$self = true;
@ -3654,7 +3658,11 @@ class CompilerBase extends \PhpAot\Core\Translator
$ce = $this->getClassEntryPtr($class);
$fn = $ce . ', ' . $this->getFuncPtr($class . '::' . $method);
$placeHolder = $this->genArray($callScope);
} else {
$fn = 'php::concat({' . $this->identifierToStr($expr->class) . ', "::", ' . $this->identifierToStr($expr->name) . '})';
$placeHolder = $fn;
}
$call = 'php::call';
if (empty($expr->args)) {
return $call . '(' . $fn . ')';

@ -0,0 +1,27 @@
--TEST--
static method
--FILE--
<?php
class Http {
public static function input($data) {
var_dump($data);
}
}
class Worker {
protected string $protocol = 'Http';
public function run() {
Http::input('http1.0');
$this->protocol::input('http1.1');
}
}
function main() {
$worker = new Worker();
$worker->run();
}
?>
--EXPECT--
string(7) "http1.0"
string(7) "http1.1"
Loading…
Cancel
Save