From 28d12102a424558b20110df8620ab7f8867391af Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Thu, 26 Feb 2026 12:02:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(php):=20=E4=BF=AE=E5=A4=8D=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E7=B1=BB=E5=9E=8B=E6=8E=A8=E6=96=AD=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E7=B1=BB=E5=90=8D=E8=A7=A3=E6=9E=90=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在类型推断过程中使用 getNamespacedClassName 方法处理类名 - 确保对象存储时使用完整的命名空间类名 - 添加了 Swoole 服务器示例代码 - 修复了 PHP 请求信息中的路径翻译设置 - 更新了 Python 示例中的打印方法调用 --- examples/python/sys.php | 10 +++++++-- examples/swoole/echo.php | 43 ++++++++++++++++++++++++++++++++++++++ examples/swoole/simple.php | 13 ++++++++++++ src/Php/CompilerBase.php | 6 +++--- src/cpp/main.cc | 2 ++ 5 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 examples/swoole/echo.php create mode 100644 examples/swoole/simple.php diff --git a/examples/python/sys.php b/examples/python/sys.php index 0858cb00..6f01698f 100644 --- a/examples/python/sys.php +++ b/examples/python/sys.php @@ -17,8 +17,14 @@ use numpy as np; function np() { - var_dump(np::$module); - var_dump(np::version->full_version); + print(np::$module); + print(np::version->full_version); } +$array["hello"] = $a; +$array["world"] = $b; + +$c = $array["world"]; + + diff --git a/examples/swoole/echo.php b/examples/swoole/echo.php new file mode 100644 index 00000000..218be4f8 --- /dev/null +++ b/examples/swoole/echo.php @@ -0,0 +1,43 @@ +mode === SWOOLE_THREAD ? \Swoole\Thread::getId() : posix_getpid(); +} + +function main() +{ + $serv = new Swoole\Server("0.0.0.0", 9501); + $serv->set([ + 'worker_num' => 2, + 'task_worker_num' => 3, + ]); + + $serv->on('workerStart', function ($serv, $worker_id) { + echo "[#" . getpid($serv) . "]\tWorker#{$worker_id} is started.\n"; + }); + + $serv->on('workerStop', function ($serv, $worker_id) { + echo "[#" . getpid($serv) . "]\tWorker#{$worker_id} is stopped.\n"; + }); + + $serv->on('connect', function ($serv, $fd, $reactor_id) { + echo "[#" . getpid($serv) . "]\tClient@[$fd:$reactor_id]: Connect.\n"; + }); + + $serv->on('receive', function (Swoole\Server $serv, $fd, $reactor_id, $data) { + echo "[#" . $serv->worker_id . "]\tClient[$fd] receive data: $data\n"; + if (!$serv->send($fd, "hello {$data}\n")) { + echo "error\n"; + } + }); + + $serv->on('close', function ($serv, $fd, $reactor_id) { + echo "[#" . getpid($serv) . "]\tClient@[$fd:$reactor_id]: Close.\n"; + }); + + $serv->on('task', function ($serv, $src_worker_id, $task) { + var_dump($task); + }); + + $serv->start(); +} diff --git a/examples/swoole/simple.php b/examples/swoole/simple.php new file mode 100644 index 00000000..cc609b2f --- /dev/null +++ b/examples/swoole/simple.php @@ -0,0 +1,13 @@ +on('receive', function (Server $serv, $fd, $reactor_id, $data) { + echo "[#" . $serv->worker_id . "]\tClient[$fd] receive data: $data\n"; + $serv->send($fd, "hello {$data}\n"); + }); + $serv->start(); +} diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index f41427ef..67bfff10 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -1156,12 +1156,12 @@ class CompilerBase extends \PhpAot\Core\Translator // 类型推断,获取对象的类名 if ($this->isNewExpr($right) and $this->isNameExpr($right->class)) { $class = $this->parseIdentifier($right->class); - $this->objects[$var] = $class; + $this->objects[$var] = $this->getNamespacedClassName($class); $type = self::TYPE_OBJECT; } elseif ($this->isFuncCallExpr($right) and $this->isNameExpr($right->name)) { $fn = $this->parseIdentifier($right->name); if (count($right->args) === 2 and $fn === 'objval' and $this->isScalarString($right->args[1]->value)) { - $this->objects[$var] = $this->parseIdentifier($right->args[1]->value); + $this->objects[$var] = $this->getNamespacedClassName($this->parseIdentifier($right->args[1]->value)); $type = self::TYPE_OBJECT; } elseif (count($right->args) === 1 and $fn === 'any') { $type = self::TYPE_VAR; @@ -1541,7 +1541,7 @@ class CompilerBase extends \PhpAot\Core\Translator $this->fatalError($param, 'Cannot use `resource` as a parameter type.'); // no break default: - $this->objects[$var] = $name; + $this->objects[$var] = $this->getNamespacedClassName($name); return self::TYPE_OBJECT; } } diff --git a/src/cpp/main.cc b/src/cpp/main.cc index b5e653ac..83e4ba27 100644 --- a/src/cpp/main.cc +++ b/src/cpp/main.cc @@ -41,6 +41,8 @@ int main(int cpp_argc, char **cpp_argv) { ProfilerStart("profile.out"); #endif try { + char path_translated[] = "embed"; + SG(request_info).path_translated = path_translated; module->request_startup_func(module->type, module->module_number); } catch (zend_object *e) { rc = EG(exit_status);