使用“使用".功能内? [英] Using "use" within a function?

查看:87
本文介绍了使用“使用".功能内?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力进行以下工作:

I'm trying to make the following work:

function flosoftdedicated_api_init() {
require_once 'resources/vendor/autoload.php';
use \Ovh\Common\Ovh;

....

$ovh = new Ovh($config);
return $ovh;
}

function flosoftdedicated_ClientArea($params) {
global $ovh;

$ovh = flosoftdedicated_api_init();
....
}

但是我得到了错误:

解析错误:语法错误,第35行..../flosoftdedicated.php中的意外T_USE 第35行是use语句.

Parse error: syntax error, unexpected T_USE in ..../flosoftdedicated.php on line 35 Line 35 being the use statement.

在函数中不能使用名称空间吗?我需要分配相同的名称空间吗?

Is it not possible to use a namespace in a function? Do I need to assign the same namespace?

推荐答案

这是因为您不能在函数中声明它.从 PHP:使用命名空间:

It's because you cannot declare it from within a function. From PHP: Using Namespaces:

必须在文件的最外部作用域(全局作用域)或内部命名空间声明中声明use关键字.这是因为导入是在编译时完成的,而不是在运行时完成的,因此无法进行块作用域的

The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped.

您必须将其移至任何函数或类之外.

You'll have to move it outside of any function or class.

这篇关于使用“使用".功能内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆