问题描述
尝试在我自己的函数中使用名称空间时出现解析错误
I get a parse error when trying to use a name space inside my own function
require('/var/load.php');
function go(){
use testClass;
$go = 'ok';
return $go;
}
echo go();
推荐答案
来自 导入范围规则
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 testClass;
function go(){
$go = 'ok';
return $go;
}
echo go();
查看以下手册中的示例 5请参阅其手册 http://php.net/manual/en/language.namespaces.importing.php
Check the example 5 in the below manual Please refer to its manual at http://php.net/manual/en/language.namespaces.importing.php
这篇关于在函数内部使用 php 命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)