modern-php (i) namespace Huawei espace computer download espace.net patents Renault Espac

Source: Internet
Author: User
Data-id= "1190000004892254" >

Name space

Declaration of the namespace

    • namespace at the top of the PHP file,

    • A namespace declaration statement begins with namespace, followed by a space, followed by the name of the namespace, ending with;

    • The vendor namespace, the "oreilly" declared below, is the most important namespace and must be globally unique.


  
   

子命名空间

 

Ps: 同一个命名空间下的所有类、接口、函数没必要在同一个PHP文件中声明;
所以,我们可以在不同的文件中编写属于同一个命名空间的多个类。

import and alias

PHP引入namespace之前,开发者们使用Zend式的类名来解决命名冲突问题;

# Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query => Zend/Cloud/DocumentService/Adapter/WindowsAzure/Query.php

但是你也看到了,太TM长了,不能忍啊!!!
namespace提供了 import和 alias来解决这个问题。
import,alias 在5.3版本下支持类,接口与命名空间导入。5.6开始支持函数与常量导入。

# namespace without alias send();$response2 = new \Symfony\Component\HttpFoundation\Response('Success',200);
# namespace with Default alias use Symfony\Component\HttpFoundation\Response;$response = new Response('Oops',400);$response->send();
# namespace with custom alias use Symfony\Component\HttpFoundation\Response as Res;$response = new Res('Oops',400);$response->send();

注意:

    • 同namespace声明一样,在PHP文件顶部使用use关键字,而且在

    • 开头无需加\符号,因为PHP导入是完全限定;

    • use必须出现在全局作用域中,因为use在编译时使用。

PHP5.6以后可以导入函数和常量;

 

导入常量:

 

实用技巧

多重导入

如果想在一个PHP文件中导入多个类、接口、函数或者常量,需要使用多个use语句;

不建议:

 

建议:

 

PHP允许一个文件定义多个命名空间【强烈不建议】

 

全局命名空间

 

NOTE: 此时,在 Exception类的名称前加\前缀是告诉PHP在全局中查找Exception,默认会在当前命名空间中查找;

The above introduces the modern-php (i) namespace, including the espace,modern aspects of the content, I hope that the PHP tutorial interested in a friend to help.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.