如何使用来自其他命名空间的对象以及如何在 PHP 中导入命名空间 [英] How to use objects from other namespaces and how to import namespaces in PHP

查看:29
本文介绍了如何使用来自其他命名空间的对象以及如何在 PHP 中导入命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两行之间的主要区别是什么?:

What is the main difference between these two lines?:

$obj = new ArrayObject();

&

$obj = new ArrayObject();

当我使用第一行时,我得到一个错误:"Fatal error: Class 'FooBarArrayObject' not found..." 我不太确定为什么我有这个错误?第二行似乎解决了问题.

When I used the first line I got an error: "Fatal error: Class 'FooBarArrayObject' not found..." and I am not too sure as to why I got this error? The second line seemed to have fixed the problem.

推荐答案

如果您使用:

$obj = new ArrayObject();

表示 ArrayObject 定义在当前命名空间中.您可以在全局命名空间(当前作用域中未定义命名空间)中或在与当前作用域相同的命名空间中定义 ArrayObject(例如 FooBar)时使用此语法.

it means that ArrayObject is defined in current namespace. You can use this syntax where you are in global namespace (no namespace defined in current scope) or if ArrayObject is defined in the same namespace as current scope (example FooBar).

如果你使用:

$obj = new ArrayObject();

表示ArrayObject定义在全局命名空间中.

it means that ArrayObject is defined in global namespace.

在你的例子中,你可能有这样的代码:

In your example you probably have code something like that:

namespace FooBar;

$obj = new ArrayObject();

它不会工作,因为你没有在 FooBar 命名空间中定义 ArrayObject.

It won't work because you haven't defined ArrayObject in FooBar namespace.

以上代码同:

namespace FooBar;

$obj = new FooBarArrayObject();

如果 ArrayObject 在全局命名空间中定义(可能在你的情况下)你需要使用代码:

And if ArrayObject is defined in global namespace (as probably in your case) you need to use code:

namespace FooBar;

$obj = new ArrayObject();

强调 ArrayObject 未在 FooBar 命名空间中定义

to accent that ArrayObject is not defined in FooBar namespace;

还有一件事 - 如果您在当前命名空间的许多地方使用 ArrayObject,则每次添加前导反斜杠可能不是很方便.这就是为什么您可以导入命名空间以便您可以使用更简单的语法:

One more thing - if you use ArrayObject in many places in your current namespace it might be not very convenient to add each time leading backslash. That's why you may import namespace so you could use easier syntax:

namespace FooBar;

use ArrayObject;

$obj = new ArrayObject();

如您所见,use ArrayObject; 是在创建对象之前添加的,以便从全局命名空间导入 ArrayObject.使用 use 您不需要添加(也不应该)添加前导反斜杠,但它的工作原理与 use ArrayObject; 相同,因此上面的代码是等效的逻辑上:

As you see use ArrayObject; was added before creating object to import ArrayObject from global namespace. Using use you don't need to add (and you shouldn't) add leading backslash however it works the same as it were use ArrayObject; so above code is equivalent logically to:

namespace FooBar;

use ArrayObject;

$obj = new ArrayObject();

然而,正如我所说,在导入命名空间时不应该使用前导反斜杠.为此引用 PHP 手册:

however as I said leading backslash in importing namespaces should not be used. Quoting PHP manual for that:

请注意,对于命名空间名称(包含命名空间分隔符的完全限定命名空间名称,例如 FooBar 而不是全局名称,例如 FooBar),前导反斜杠是不必要的,不推荐使用,因为导入名称必须完全限定,并且不会相对于当前命名空间进行处理.

Note that for namespaced names (fully qualified namespace names containing namespace separator, such as FooBar as opposed to global names that do not, such as FooBar), the leading backslash is unnecessary and not recommended, as import names must be fully qualified, and are not processed relative to the current namespace.

这篇关于如何使用来自其他命名空间的对象以及如何在 PHP 中导入命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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