现代PHP的新特性(2) – 声明命名空间

Declaration

Every PHP class,interface,function,and constant lives beneath a namespace(or subnamespace).namesplace are declared at the top of a PHP file on a new line immediately after the opening <?php tag.the namespace declararion begins with namespace,then a space character,then the namespace name,and then a closing semicolon;character.

声明
每个PHP类,接口,函数和变量均在命名空间的定义之下,命名空间的声明位于PHP文件顶部的<?php标签下方,命名空间声明namespace之后是一个空格,然后是命名空间的名称,最后由分号结束.

Remember that namespaces are often used to establish a top-level vendor name.this example namespace declaration establishes the oreilly vendor name:

记住,命名空间通常用来建立组件的顶级名称,举个例子,下面的代码声明并创建了oreilly组件的命名空间.

<?php
Namespace Oreilly;

All PHP classes,interfaces,functions,or constants declared beneath this namespace declaration live in the Oreilly namespace and are,in some way,related to O`Reilly Media.what if we wanted to organize code related to this book?we use a subnamespace.

现在,所有PHP类,接口,函数或变量的命名空间均为Oreilly,如果我们还需要进一步细分时,如O`Reilly Media,应该如何组织这些代码呢?我们可以使用subnamespace

Subnamespaces are declared exactly the same as in the previous example.the only difference is that we separate namespace and subnamespace names with the \ character.the following example declares a subnamespace named modern PHP that lives beneath the topmost Oreilly vendor namespace.

Subnamespace声明与namespace几乎完全相同,举个例子,他们之间唯一的不同之处在于命名空间与subnamespace的名称在\符号前后相互独立.下面的例子在顶级命名空间Oreilly组件之下声明了一个subnamespace modernPHP

<?php
Namespace Oreilly\ModernPHP

All classes,interface,functions,and constants declared beneath this namespace declaration live in the Oreilly\ModernPHP subnamespace and are,in some way,related to this book.

All classes in the same namespace or subnamespace don`t have to be declared in the same PHP file.you can specify a namespace or subnamespace at the top of any PHP file,and that file`s code becomes a part of that namespace or subnamespace,this makes it possible to write multiple classes in separate files that belong to a common namespace.

现在,所有类,接口,函数和变量均在命名空间oreilly和subnamespace ModernPHP之下

在同一个命名空间下的类不需要编写在同一个PHP文件中,你可以为任何PHP文件指定命名空间,这样文件的代码就成了某个命名空间的一部分,这样做使我们可以让多个文件中的多个类使用同一命名空间.

Import and alias

Beforce we had namespaces,PHP developers solved the name collision problem with Zend-style class names.this was a class-naming scheme popularized by zend Framework where PHP class name used underscores in lieu of filesystem directory separators,this convention accomplished two things:it ensured class names were unique,and it enabled a naive autoloader implementation that replaced underscores in PHP class names with filesystem directory separators to determine the class file path.

导入和别名

在命名空间出现之前,PHP开发者使用zend风格的命名方式解决命名冲突的问题,这类类似文件系统目录隔离的命名方式通过zend框架进行了普及.这种方式确保了两件事:确保类名唯一,允许自动加载程序像文件系统那样对PHP类名替换下划线,并对其隔离.

For example,the PHP class zend_cloud_documentService_adapter_windowsAzure_query corresponds to the PHP file zend/cloud/documentservice/adspter/widowsazure/query.php.a side effect of the zend-style naming convertion,as you can see,is absurdly long class names,call me lazy,but there`s no way i`m typing that class name more than once.

举个例子,PHP类zend_cloud_documentService_adapter_windowsAzure_query corresponds位于文件zend/cloud/documentservice/adspter/widowsazure/query.php,这种zend风格的命名方式有一个副作用,就是会产生一个非常长且古怪的类名,虽然我很懒,但我不得不经常键入这种风格的类名.
Modern PHP namespaces present a similar problem.for example,the full response class name in the syfony\httpfoundation component is \symfony\component\httpFoundation\Response.fortunately PHP lets us import and alias namespaced code.

现代PHP的命名空间也有类似的问题,举个例子,一个命名为symfony\httpfoundation的组件,位于\Symfony\Component \HttpFoundation\Response,幸运的是PHP为我们导入并为命名空间建立了别名

By import,I mean that I tell PHP which namespaces,classes,interfaces,functions,and constants I will use in each PHP file,I can then use there without typing their full namespaces.

导入文件时,我可以告诉PHP我需要使用的每个PHP文件,而不需完整的输入他们的完整命名空间

By alias,I mean that I tell PHP that I will reference an imported class,interface,function,or constant with a shorter name.

导入文件时,我可以使用一个较短的名称

The code shown in Example 2-1 creates and sends a 400 bad request HTTP response without importing and aliasing

示例2-1,在不使用包含和别名的情况下,创建并发送一个400错误

Example 2-1,namespace without alias

<?php
$response = new \symfony\component\httpfoundation\response(‘oops’,400);
$response->send();

This isn`t terrible,but imagine you have to instantiate a response instance several times in a single PHP file.you fingers will get tired quickly.now look at example 2-2.it does the same thing with importing.

这样做并不是太恐怖,但是你每次都需要在每个PHP中实例化response实例,你的手指很快就会感到疲劳,现在我们来看看示例2-2,他用引入达到了同样的目的

Example 2-2.namespace with default alias

<?php

Use symfony\component\httpfoundation\response;

$response = new Response(‘oops’,400);
$response->send();

We tell PHP we intend to use the symfony\component\httpfoundation\response class with the use keyword.we type the long,fully qualified class name once.then we can instantiate the response class without using its fully namespaced class name.how cool is that?

Some days I feel really lazy,this is a good opportunity to use an alias,Let`s extend Example 2-2,instead of typing response,maybe response,maybe,I just want to type res instead.example 2-3 shows how I can do that

我们告诉PHP将要使用symfony\component\httpfoundation\response类,我们只需要完整的输入一次该类的命名空间,然后在实例化的时候就不必输入完整的命名空间路径了,这样很酷吧?

有些时候,我真的感到非常疲倦,这是使用别名的好时机,我们扩展示例2-2,替换掉response,比如我们希望用res来替换他,示例2-3将会展示如何实现

Example 2-3 namespace with custom alias

<?php

Use syfony\component\httpfoundation\response as res;

$r = new Res(‘oops’,400);
$r->send();

In this example,I changed the import line to import the response class.I also appended as Res to the end of the import line;this tells PHP to consider Res an alias for the Response class.If we don`t append the as Res alias to the import line,PHP assumes a default alias that is the same as the imported class name.

在这个示例中,我修改了导入response这一行的代码,我在代码的末尾添加了 as res,他告诉PHP把res作为response的别名,如果我们没有这么做,PHP会将默认别名设置为与我们导入的类名一样.

As of PHP5.6,it`s possible to import functions and constants.this requires a tweak to the use keyword syntax.to import a function,change use to use func.

在PHP5.6之后,我们也可以导入函数的常量了,我们只需要稍微调整关键词语法

<?php
Use func namespace\functionname;

Functionname();

To import a constant,change use to constant;
如果需要导入常量,我们需要将关键词修改为constant

<?php
Use constant namespace\CONST_NAME;

Echo CONST_NAME;

Function and constant aliases work the same as classes.
函数和常量都可以像类一样使用别名