PHP:"use"在包含文件? [英] PHP: "use" in include files?

查看:142
本文介绍了PHP:"use"在包含文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中使用命名空间时,您可以执行以下操作:

When using namespaces in PHP you can do things like:

<?php
use \mynamspace\MyClass;
$a = new MyClass();
?>

是否可以将use ...行放在包含文件中?如果有几十个这些use语句,那么将它们写到您需要的每个文件中就很烦人了.

Is it possible to put the use... line in an include file? If there are dozens oft those use statements it is very annoying to write them in every single file you need them.

但是use语句仅适用于当前文件,并且在包含它的文件中被忽略.

But the use statement is only working for the current file and is ignored in the files that include it.

有什么想法吗?

推荐答案

不幸的是没有. PHP的名称空间构造是基于每个文件的. PHP手册总结如下:

Unfortunately not. PHP's namespace constructs are in a per file basis. The PHP manual sums it up as such:

导入规则是基于文件的,这意味着包含的文件不会 继承父文件的导入规则.

Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.

http://php.net/manual/en/language.namespaces. importing.php

这篇关于PHP:"use"在包含文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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