fix(php): 修复对象类型推断中的类名解析问题

- 在类型推断过程中使用 getNamespacedClassName 方法处理类名
- 确保对象存储时使用完整的命名空间类名
- 添加了 Swoole 服务器示例代码
- 修复了 PHP 请求信息中的路径翻译设置
- 更新了 Python 示例中的打印方法调用
pull/1/head
韩天峰 6 months ago
parent 24ea4d9806
commit 28d12102a4
  1. 10
      examples/python/sys.php
  2. 43
      examples/swoole/echo.php
  3. 13
      examples/swoole/simple.php
  4. 6
      src/Php/CompilerBase.php
  5. 2
      src/cpp/main.cc

@ -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"];

@ -0,0 +1,43 @@
<?php
function getpid($serv)
{
return $serv->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();
}

@ -0,0 +1,13 @@
<?php
use Swoole\Server;
function main()
{
$serv = new Server("0.0.0.0", 9501);
$serv->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();
}

@ -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;
}
}

@ -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);

Loading…
Cancel
Save