现代PHP的新特性(1) – 命名空间

Features

 

The modern PHP language has many exciting new features.many of there features will be brand new to PHP programmers upgrading from earlier versions,and they`ll be a nice surprise to programmers migrating to PHP from another language.there new feature make the PHP language a powerful platform and provide a pleasant experience for building web applications and command-line tools.

 

Some of there features aren`t essential,but they still make our lives easier.some features,however,are essential.namespaces,for example,are a cornerstone of modern PHP standards and enable development practices that modern PHP developers take for granted.I`ll introduce each new feature,explain why it is useful,and show you how to implement it in your own projects.

 

特性

 

现代PHP语言拥有许多令人兴奋的特性,很多新特性已经包含在新版本之中.对于从其他语言转入的程序员感到惊奇.新特性将PHP打造成一个健壮的平台,同时为应用程序和命令行工具开打带来了更加舒适的体验.

 

虽然能够为我们带来便捷,但这些新特性并不会强制我们使用他.但是有一些特性是强制的,举个例子,命名空间是现代PHP的基石,并且允许PHP开发者在产品相互让步.我会采用很多新特性来解释为什么这是很常用的?并且告诉你如何在自己的项目中实践这些特性.

 

Namespace

 

If there is one modern PHP feature I want you to know,it is namespaces.introduced in PHP 5.3.0,namespaces are an important tool that organizes PHP code into a virtual hierarchy,comparable to your operating system`s filesystem directory structure.each modern PHP component and framework organizes its code beneath its own globally unique vendor namespace so that it does not conflict with,or lay claim to,common class names used by other vendors.

 

命名控件是一个必须得了解的PHP新特性,命名空间在PHP5.3.0引入,这是一个非常重要的PHP代码分层工具,相当于操作系统的文件目录体系,每个PHP组件和框架均可以标识出公有且唯一的命令空间,这样不会因为不专业的声明或公共类命令重复而引发冲突.

 

Let`s see how a real-world PHP component uses namespaces. The symfony framework`s own symfony/httpfoundtion is a popular PHP component that manages HTTP requests and responese.more important,the syfony/httpfoundation component uses common PHP class names like question,response,and cookie.i guarantee you there are many other PHP components that use there same class names.how can we use the symfony/httpfoundation PHP component if other PHP code uses the same class name?we can safely use the symfony/httpfoundation component precisely because its code is sandboxed beneath the unique symfony verndor namesapce.visit the syfony/httpfoundation component on github and navigate to the response.php file.it looks like figure 2-1.

 

我们看看如何在现实中运用命名空间,symfony框架自身的symfony/httpfoundtion是一个普通的PHP组件,用于管理HTTP请求和回应.更重要的是symfony/httpfoundtion使用了很多常用的PHP类名如question,response和cookie.我保证会有很多其他PHP组件使用同样的类名,如何在其他PHP组件使用了相同的类名的情况下,使用symfony/httpfoundtion组件呢?我们当然可以安全且精确的使用symfony/httpfoundtion组件,因为代码在沙盒机制下,symfony这个产品拥有唯一的命令空间.查看github下的syfony/httpfoundation组件找到respones.php文件,你会看到(图片2-1)

 

This is a PHP namespace declaration,and it always appears on a new line immediately atfer the opening <? Php tag.this particular namespace declaration tells us several things.first,we know the response class lives beneath the syfony vendor namespace(the vendor namespace is the topmost namespace).we know the response class lives beneath the component subnamespace.we also know the response class lives beneath yet another subnamespace named httpfoundation,you can view other files adjacent to response.php,and you`ll see they use the same namespace declaration.a namespace(or subnamespace) encapsulates and organizes related PHP classes,just as a filesystem directory contains related files.

 

这是PHP命名空间的声明,他一般位于开始标签<?php之后,这行代码告诉我们几件事:(1)我们知道response类位于syfony产品命名空间的的下方(产品命名空间是最顶级的命名空间)(2)我们知道response类同样位于命名空间httpfoundaction之下,你可以查看response.php的相邻文件,你会发现他们都使用了相同的命名空间声明来封装和组织PHP类,就像文件系统目录包含相关文件一样.

 

Unlike your operating system`s physical filesystem,PHP namespaces are a virtual concept and do not necessarily map 1:1 with filesystem directories.that being said,most PHP componeents do,in fact,map subnamesapce to filesystem directories for compatibillity with the popular PSR-4 autoloader standard(we`ll talk more about this in chapter 3)

 

不同的是,PHP命名空间是一个虚拟概念,与操作系统的物理文件系统并不完全一致,事实上大多PHP组件的一级命名空间和文件系统目录的映射关系是类似的.并使用主流的PSR-4自动加载标准(我们将在第三章详细讲解)

 

Why we use namespaces

 

Namespaces are important because they let us create sandboxed code that works alongside other developers` code.this is the cornerstone concept of the modern PHP component ecosystem component and framework authors build and distribute code for a large number of PHP developers,and they have no way of knowing or controlling what classes,interfaces,functions,and constants,too.if you write custom PHP components or classes for a project,that code must work alongside your project`s third-party dependencies.

 

为什么使用命名空间?

命名空间非常重要,因为我们可以借助他创建沙盒用户运行其他开发者的代码.这个概念最初应用于MAC系统,PHP开发者编译,分发了庞大的PHP组件和框架,他们没有合适的方式管理和控制类,函数,变量,如果你在项目中编写了自定义PHP组件或类,这些代码必须与第三方代码独立运行.

 

namespaces,a name collision causes PHP to fail.with namespaces,your code and other developers` code can use the same class,interface,function,or constant name assuming your code lives beneath a unique vendor namespace.

 

因为命名导致PHP出错,通过使用命名空间,就算你和其他开发者在代码之中使用同样的类,函数,变量名称,他们也可以保持唯一.

 

If you`re building a tiny personal project with only a few dependencies,class name collision probably won`t be an issue.but when you`re working on a team building a large project with numerous third-party dependencies,name collisions become a very real concern.you cannot control which classes,interfaces,functions,and constants are introduced into the global namespace by your project`s dependencies.this is why namespacing your code is important.

 

如果你只是创建一个小型项目,那么类名冲突的可能较小,但是当你就职于一个团队,并且在大型项目中使用了众多第三方组件,那么命名冲突就成为了一个非常现实的问题,你无法将类,接口,函数或变量引入项目的公共的命名空间之中,这就是命名空间的重要性.