PHP 命名空间和“使用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10542012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:25:33  来源:igfitidea点击:

PHP namespaces and "use"

phpnamespaces

提问by Shawn Northrop

I am having a little trouble with namespaces and the usestatements.

我在命名空间和use语句方面遇到了一些麻烦。

I have three files: ShapeInterface.php, Shape.phpand Circle.php.

我有三个文件:ShapeInterface.php,Shape.phpCircle.php.

I am trying to do this using relative paths so I have put this in all of the classes:

我试图使用相对路径来做到这一点,所以我把它放在所有的类中:

namespace Shape; 

In my circle class I have the following:

在我的圈子课中,我有以下内容:

namespace Shape;
//use Shape;
//use ShapeInterface;

include 'Shape.php';
include 'ShapeInterface.php';    

class Circle extends Shape implements ShapeInterface{ ....

If I use the includestatements I get no errors. If I try the usestatements I get:

如果我使用这些include语句,我不会出错。如果我尝试use我得到的陈述:

Fatal error: Class 'Shape\Shape' not found in /Users/shawn/Documents/work/sites/workspace/shape/Circle.php on line 8

致命错误:在第 8 行的 /Users/shawn/Documents/work/sites/workspace/shape/Circle.php 中找不到类“Shape\Shape”

Could someone please give me a little guidance on the issue?

有人可以就这个问题给我一些指导吗?

回答by cmbuckley

The useoperatoris for giving aliases to names of classes, interfaces or other namespaces. Most usestatements refer to a namespace or class that you'd like to shorten:

use运算符用于为类、接口或其他命名空间的名称提供别名。大多数use语句引用您想要缩短的命名空间或类:

use My\Full\Namespace;

is equivalent to:

相当于:

use My\Full\Namespace as Namespace;
// Namespace\Foo is now shorthand for My\Full\Namespace\Foo

If the useoperator is used with a class or interface name, it has the following uses:

如果use运算符与类或接口名称一起使用,则它具有以下用途:

// after this, "new DifferentName();" would instantiate a My\Full\Classname
use My\Full\Classname as DifferentName;

// global class - making "new ArrayObject()" and "new \ArrayObject()" equivalent
use ArrayObject;

The useoperator is not to be confused with autoloading. A class is autoloaded (negating the need for include) by registering an autoloader (e.g. with spl_autoload_register). You might want to read PSR-4to see a suitable autoloader implementation.

use不要将运算符与autoloading混淆。include通过注册一个自动加载器(例如使用spl_autoload_register)来自动加载一个类(不需要)。您可能需要阅读PSR-4以查看合适的自动加载器实现。

回答by Charlie

If you need to order your code into namespaces, just use the keyword namespace:

如果您需要将代码排序到命名空间中,只需使用关键字namespace

file1.php

文件1.php

namespace foo\bar;

In file2.php

在 file2.php 中

$obj = new \foo\bar\myObj();

You can also use use. If in file2 you put

您也可以使用use. 如果在 file2 你把

use foo\bar as mypath;

use foo\bar as mypath;

you need to use mypathinstead of baranywhere in the file:

您需要使用mypath而不是bar文件中的任何位置:

$obj  = new mypath\myObj();

Using use foo\bar;is equal to use foo\bar as bar;.

使用use foo\bar;等于use foo\bar as bar;