在函数内部使用php命名空间 [英] Use php namespace inside function

查看:112
本文介绍了在函数内部使用php命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试在我自己的函数中使用名称空间时出现分析错误

I get a parse error when trying to use a name space inside my own function

require('/var/load.php');

function go(){

  use test\Class;

    $go = 'ok';
    return $go;
}

    echo go();


推荐答案

From 用于导入的范围规则


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

所以你应该像这样,使用应该在全球层面指定

So you should put like this, use should specified at the global level

require('/var/load.php');
use test\Class;

function go(){
    $go = 'ok';
    return $go;
}
echo go();

检查下面的手册
中的示例5请参阅其手册 http://php.net/manual/en/language.namespaces.importing.php a>

Check the example 5 in the below manual Please refer to its manual at http://php.net/manual/en/language.namespaces.importing.php

这篇关于在函数内部使用php命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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