重置PHP中的名称空间? [英] Resetting a namespace in PHP?

查看:70
本文介绍了重置PHP中的名称空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将名称空间重置"为全局名称空间?给出以下代码:

How can I "reset" a namespace to the global one? Given the following code:

namespace foo;
include 'myfile.php';

myfile.php现在将尝试将其所有类加载到foo名称空间中,即使其类位于全局名称空间中也是如此.现在交换这些行的顺序不是什么大问题,但是我将如何处理具有自动加载器的myfile.php?它将尝试将类加载到命名空间foo中.

myfile.php will now try to load all of its classes in the foo namespace, even though its classes are in the global namespace. Now it wouldn't be a big deal to swap the order of those lines, but how would I deal with myfile.php having an autoloader? It will try to load the classes in namespace foo.

推荐答案

命名空间以每个文件为基础.如果myfile.php没有声明任何名称空间,则其中的所有内容都将属于全局名称空间,而与使用include的名称空间无关.

Namespaces work on a per-file basis. If myfile.php doesn't declare any namespace then everything in it will belong to the global namespace, regardless of the namespace in which include was used.

长话短说,namespace声明仅适用于自己的文件,不包括.

Long story short, namespace declarations only apply to their own file, not includes.

这篇关于重置PHP中的名称空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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