From 51f039e6b4b7f8a77c1e9b7d236475af9362b2bc Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 22 Jan 2026 18:23:40 +0800 Subject: [PATCH] =?UTF-8?q?refactor(Php):=20=E4=BC=98=E5=8C=96=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E5=80=BC=E8=A7=A3=E6=9E=90=E9=80=BB=E8=BE=91=E5=B9=B6?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=B9=E6=B3=95=E6=A0=87=E8=AF=86=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 简化 CompilerBase.php 中的对象值解析条件判断 - 在 FunctionDef.php 中添加 method 属性用于标识是否为方法 - 在 MethodDef.php 构造函数中设置 functionDef 的 method 标志 - 在 Translator.php 参数列表生成时为方法添加 this_ 对象参数 --- src/Php/CompilerBase.php | 7 +++---- src/Php/FunctionDef.php | 1 + src/Php/MethodDef.php | 1 + src/Php/Translator.php | 3 +++ 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 3cf3ff7d..54f6dcd2 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -844,11 +844,10 @@ class CompilerBase extends \PhpAot\Core\Translator $type = self::TYPE_OBJECT; } elseif ($this->isFuncCallExpr($right) and $this->isNameExpr($right->name)) { $fn = $this->parseIdentifier($right->name); - $arg2 = $right->args[1]->value; - if (count($right->args) === 2 and $fn === 'objval' and $this->isScalarString($arg2)) { - $this->objects[$var] = $arg2->value; + if (count($right->args) === 2 and $fn === 'objval' and $this->isScalarString($right->args[1]->value)) { + $this->objects[$var] = $right->args[1]->value; + $type = self::TYPE_OBJECT; } - $type = self::TYPE_OBJECT; } if (!$this->hasVar($var)) { diff --git a/src/Php/FunctionDef.php b/src/Php/FunctionDef.php index 91403f17..bf737207 100644 --- a/src/Php/FunctionDef.php +++ b/src/Php/FunctionDef.php @@ -9,6 +9,7 @@ class FunctionDef public array $argInfoList = []; public int $argCountRequired = 0; public string $params = ''; + public bool $method = false; public function __construct(string $name, string $returnType) diff --git a/src/Php/MethodDef.php b/src/Php/MethodDef.php index 900c7826..11c1d15c 100644 --- a/src/Php/MethodDef.php +++ b/src/Php/MethodDef.php @@ -13,6 +13,7 @@ class MethodDef $this->flags = $flags; $this->name = $name; $this->functionDef = $def; + $this->functionDef->method = true; } public function getReturnType(): string diff --git a/src/Php/Translator.php b/src/Php/Translator.php index d54089e9..24f3d3c2 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -426,6 +426,9 @@ class Translator extends Preprocessor $argInfoList = $func->argInfoList; if ($argInfoList) { $list = []; + if ($func->method) { + $list[] = 'php::Object &this_'; + } foreach ($argInfoList as $argInfo) { $arg = $argInfo->type . ' ' . $argInfo->name; if ($argInfo->default) {