现代PHP的新特性(3) – 有用的提示

Helpful tips

Multiple imports

If you import multiple classes,interface,functions,or constants into a single PHP file,you’ll end up with multiple use statements at the top of your PHP file.PHP accepts a shorthand import syntax that combines multiple use statements on a single line like this:

有用的建议
多个导入

如果你需要导入多个类,接口,函数或常量到一个单独的PHP文件,那么你最终需要多次在PHP的顶部进行声明,PHP支持速记语法,即在一行合并多次声明,就像这样:

<?php
Use Symfony\component\Httpfoundation\request,
Symfony\component\HttpFoundaction\Response,
Symfony\Component\HttpFoundaction

Don’t do this,It’s confusing and easy to mess up,I recommend you keep each use statement on its own line like this:

不过不推荐这样做,这会造成混乱和费解,我推荐你保持每个声明独立一行,就像这样:

<?php
Use Symfony\Component\HttpFoundation\Request;
Use Symfony\Component\HttpFoundtion\Response;
Use Symfony\Component\Cookie;

You’ll type a few extra characters,but your code is easier to read and troubleshoot.

你将会键入少许额外的字符,但是会使你的代码更简单也更容易进行故障诊断

Multiple namespaces in one file
PHP lets you define multiple namespaces in a single PHP file like this:

<?php
Namespace Foo{
//declare classes,interfaces,functions,and constants here
}

Namespace Bar{
//declare classes,interfaces,functions,and constants here
}
This is confusing and violates the recommended one class per file good practice.use only one namespace per file to make your code simpler and easier to troubshoot.

这样会令人感到混乱和费解,一个文件只包含一个类是很好的做法,一个文件值拥有一个命名空间也能让你的代码简洁并便于故障诊断

Global namespace

If you reference a class,interface,function,or constant without a namespace,PHP assumes the class,interface,function,or constant lives in the current namespace.if this assumption is wrong,PHP attempts to resolve the class,interface,function,or constant.if you need to referencce a namespace class,interface,function,or constant inside another namespace,you must use the fully qualified PHP class name,or you can import the code into the current namespace with the use keyword.

公有命名空间

如果你引用的类,接口,函数或者常量没有包含命名空间,PHP会假设这些类,接口,函数或者常量存在于与当前命名空间中.如果假设是错误的,PHP会尝试加载这个类,接口,函数或常量,如果你需要引入一个位于另一个命名空间中的类,接口,函数或常量,你必须在导入代码中使用完整的关键词描述这些类,接口,函数或常量.

Some code might not have a namespace and,thereforce,lives in the global namespace.the native exception class is a good example.you can reference globally namespaced code inside another namespace by prepending a \ character to the class,interface,function,or constant name,for example,the \My\App\Foo::dosomething() method in Example 2-4 fails beccause PHP searched for a \My\App\exception class that does not existe.

一些代码可能没有包含命名空间,所以会存在于公共命名空间中,异常堆栈是一个很好的例子,你可以把一个公共的命名空间的代码放入另一个命名空间,然后在类,接口,函数或常量前面加上字符\,举个例子在实例2-4中的\My\app\foo::dosomething方法失败了,因为PHP发现\My\App\exception 类并不存在.

Example 2-4 unqualified class name inside another namespace

实例2-4 将无命名看空间的类放入另一个命名空间

<?php
Namespace My\App;
Class Foo{
Public function dosomething()
{
$exception = new Exception();
}
}

Instead,add a \ prefix to the Exception class name,as shown in Example 2-5.this tells PHP to look for the Exception class in the global namespoace instead of the current namespace.

反例,2-5,添加字符\到exception类的前面,告诉PHP去公共命名空间中去查找exception类,而不是在当前命名空间中查找

Example 2-5 qualified class name inside another namespace

<?php

Namespace My\App;

Class Foo{
Public function dosomething()
{
Throw new \exception();
}
}

Autoloading

自动加载

Namespace also provide the bedrock for the PSR4 autoloader standard created by the PHP framework interop Group(PHP-FIG).this autoloader pattern is used by most modern PHP components,and it lets us autoload project dependencies using the composer dependency manager,we’ll talk about composer and the PHP-FIG in chapter-4.for now,just understand that the modern PHP ecosystem and its emerging component-based architecture would be impossible without namespace.

命名空间是基于PSR4标准,使用通用语言运行时(CLR)概念构造的PHP框架基石,这个自动加载模式在大多数现代PHP的重要部件,他使得我们可以通过composer依赖关系管理工具自动加载其他附属项目,我们会在第四章详细讨论composer和PHP-FIG,现在我们只需要知道他们是PHP生态系统的新的关键组成部分,现代PHP体系离不开命名空间.