1:命名空间的命名不区分大小写

2:namespace必须在所有代码之前,除了declare语法以外(不过他之前可以有注释,空行等)

3:只有以下类型的代码受命名空间的影响,它们是:类(包括抽象类和traits)、接口、函数和常量。

4:如果你需要定义一个常量只在当前命名空间中,定义的时候要加上命名空间前缀,否则定义的是存在于全局命名空间的常量

例子:

The following code will define the constant "MESSAGE" in the global namespace (i.e. "\MESSAGE").

namespace test;
define('MESSAGE', 'Hello world!');

The following code will define two constants in the "test" namespace.

namespace test;
define('test\HELLO', 'Hello world!');
define(__NAMESPACE__ . '\GOODBYE', 'Goodbye cruel world!');
<?php
namespace NS; define(__NAMESPACE__ .'\foo','111');
define('foo','222'); echo foo; // 111.
echo \foo; // 222.
echo \NS\foo // 111.
echo NS\foo // fatal error. assumes \NS\NS\foo.
?>

5:The namespace hierarchy will normally mirror the directory hierarchy where the class files are located, but this is not a strict requirement. You could, for example, have several class flles with different namespaces in the same directory.

6: \ABC\__DOMAIN__ 和  ABC\__COMAIN__的区别

\ABC\__DOMAIN__指的是以根命名空间为起点的,而不加\默认以当前所在文件的命名空间为起点, 解析时会默认加上当前文件的命名空间

这个同文件系统的相对路径和绝对路径

baseTest.php
<?php
namespace ABC;
const __DOMAIN__ = 'example.com';

test.php

<?php
namespace ABC\SUBLEVEL;
require 'baseTest.php';
echo \ABC\__DOMAIN__; //输出example.com (测试一:加\)

测试二(不加\)

<?php
namespace ABC\SUBLEVEL;
require 'baseTest.php';
const __DOMAIN__ = 'self.com';
echo ABC\__DOMAIN__; //输出example.com

7:注意在命名空间中用exception类时

<?php

namespace Foo;

try {
// Something awful here
// That will throw a new exception from SPL
}
catch (Exception as $ex) {
// We will never get here
// This is because we are catchin Foo\Exception
}
?> Instead use fully qualified name for the exception to catch it <?php namespace Foo; try {
// something awful here
// That will throw a new exception from SPL
}
catch (\Exception as $ex) {
// Now we can get here at last
}
?>

8:一个文件中的内容的命名空间与include包含无关,只跟当前的文件有关

如果没有定义任何命名空间,所有的类与函数的定义都是在全局空间,与 PHP 引入命名空间概念前一样。在名称前加上前缀 \ 表示该名称是全局空间中的名称,即使该名称位于其它的命名空间中时也是如此。

Included files will default to the global namespace.
<?php
//test.php
namespace test {
include 'test1.inc';
echo '-',__NAMESPACE__,'-<br />';
}
?> <?php
//test1.inc
echo '-',__NAMESPACE__,'-<br />';
?> Results of test.php: --
-test-

9:

在一个命名空间中,当 PHP 遇到一个非限定的类、函数或常量名称时,它使用不同的优先策略来解析该名称。类名称总是解析到当前命名空间中的名称。因此在访问系统内部或不包含在命名空间中的类名称时,必须使用完全限定名称

对于函数和常量来说,如果当前命名空间中不存在该函数或常量,PHP 会退而使用全局空间中的函数或常量。

<?php
namespace A\B\C;
class Exception extends \Exception {} $a = new Exception('hi'); // $a 是类 A\B\C\Exception 的一个对象
$b = new \Exception('hi'); // $b 是类 Exception 的一个对象 $c = new ArrayObject; // 致命错误, 找不到 A\B\C\ArrayObject 类
?>
<?php
namespace A\B\C; const E_ERROR = 45;
function strlen($str)
{
return \strlen($str) - 1;
} echo E_ERROR, "\n"; // 输出 "45"
echo INI_ALL, "\n"; // 输出 "7" - 使用全局常量 INI_ALL echo strlen('hi'), "\n"; // 输出 "1"
if (is_array('hi')) { // 输出 "is not array"
echo "is array\n";
} else {
echo "is not array\n";
}
?>

10:相同的命名空间不能出现在一个文件中,但是包含进来的相同命名空间则没问题

file1.php

<?php
namespace my\stuff;
class MyClass {}
?>

another.php

<?php
namespace another;
class thing {}
?>

file2.php

<?php
namespace my\stuff;
include 'file1.php';
include 'another.php'; use another\thing as MyClass;
$a = new MyClass; // instantiates class "thing" from namespace another
?>

There is no name conflict, even though the class MyClass exists within the my\stuff namespace, because the MyClass definition is in a separate file. However, the next example causes a fatal error on name conflict because MyClass is defined in the same file as the use statement.

<?php
namespace my\stuff;
use another\thing as MyClass;
class MyClass {} // fatal error: MyClass conflicts with import statement
$a = new MyClass;
?>

11:嵌套的命名空间不允许

<?php
namespace my\stuff {
namespace nested {
class foo {}
}
}
?>

However, it is easy to simulate nested namespaces like so:

<?php
namespace my\stuff\nested {
class foo {}
}
?>

12:动态命名空间的名字需要主要反斜线的转义

<?php
$a = new "dangerous\name"; // \n is a newline inside double quoted strings!
$obj = new $a; $a = new 'not\at\all\dangerous'; // no problems here.
$obj = new $a;
?>

注意:Inside a single-quoted string, the backslash escape sequence is much safer to use, but it is still recommended practice to escape backslashes in all strings as a best practice.

13:常量如果没有定义,则返回notice错误,默认常量的值设置成和它的名字一样

  命名空间如果没有定义,则返回fatal错误

<?php
namespace bar;
$a = FOO; // produces notice - undefined constants "FOO" assumed "FOO";
$a = \FOO; // fatal error, undefined namespace constant FOO
$a = Bar\FOO; // fatal error, undefined namespace constant bar\Bar\FOO
$a = \Bar\FOO; // fatal error, undefined namespace constant Bar\FOO
?>

php分享二十九:命名空间的更多相关文章

  1. Web 开发人员和设计师必读文章推荐【系列二十九】

    <Web 前端开发精华文章推荐>2014年第8期(总第29期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...

  2. VMwarevSphere 服务器虚拟化之二十九 桌面虚拟化之安装View副本服务器

    VMwarevSphere 服务器虚拟化之二十九  桌面虚拟化之安装View副本服务器 VMware View中高可用性可是一个必须要考虑的问题.在整个虚拟桌面环境中View Connection S ...

  3. Citrix服务器虚拟化之二十九 XenApp 6.5发布服务器上的应用程序

    Citrix服务器虚拟化之二十九  XenApp 6.5发布服务器上的应用程序 XenApp可发布以下类型的资源向用户提供信息访问,这些资源可在服务器或桌面上虚拟化: 1)  服务器桌面:发布场中服务 ...

  4. 剑指Offer(二十九):最小的K个数

    剑指Offer(二十九):最小的K个数 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/baid ...

  5. Bootstrap <基础二十九>面板(Panels)

    Bootstrap 面板(Panels).面板组件用于把 DOM 组件插入到一个盒子中.创建一个基本的面板,只需要向 <div> 元素添加 class .panel 和 class .pa ...

  6. WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载]

    原文:WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载] 我们有两种典型的WCF调用方式:通过SvcUtil.exe(或者添加Web引用)导入发布的服务元数据生成服务代理相关的代码 ...

  7. Bootstrap入门(二十九)JS插件6:弹出框

    Bootstrap入门(二十九)JS插件6:弹出框 加入小覆盖的内容,像在iPad上,用于存放非主要信息 弹出框是依赖于工具提示插件的,那它也和工具提示是一样的,是需要初始化才能够使用的 首先我们引入 ...

  8. mysql进阶(二十九)常用函数

    mysql进阶(二十九)常用函数 一.数学函数 ABS(x) 返回x的绝对值 BIN(x) 返回x的二进制(OCT返回八进制,HEX返回十六进制) CEILING(x) 返回大于x的最小整数值 EXP ...

  9. JAVA之旅(二十九)——文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习

    JAVA之旅(二十九)--文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习 我们继续学习File 一.文件递归 我们可以来实现 ...

随机推荐

  1. Nginx源代码分析—业务流程

    Nginx源代码分析-业务流程 到此为止,我们如果ngx_init_cycle已经结束.我们临时无论他做了什么,我们从他做的效果进入. 从常理上来讲,假设一个请求到达,那么我们须要接受这个请求,那么就 ...

  2. Java内存区域与各区域OOM

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6534990.html  JVM的组成在上一篇博文已经介绍了,现在我们专门深入Java运行时数据区. 1:程序计 ...

  3. gzip:stdin:not in gzip format的解决办法

    执行解压命令,在解压.gz或者.bz2格式的文件的文件的时候可能会出现这样的错误提示 tar -zxvf rlwrap-0.30.tar.gz 报错如下 gzip: stdin: not in gzi ...

  4. 【DB2】SQL优化

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAA

  5. redis对key的基本操作

    不忘初心,励志前行 del key1 key2 ... Keyn作用: 删除1个或多个键返回值: 不存在的key忽略掉,返回真正删除的key的数量 rename key newkey作用: 给key赋 ...

  6. 最常用的Java库一览(13年的文章)

    来源于:http://www.importnew.com/7530.html 本文由 ImportNew - 邢 敏 翻译自 programcreek.欢迎加入翻译小组.转载请见文末要求. 写在前面: ...

  7. Kubernetes的Cron Job

    Kubernetes集群使用Cron Job管理基于时间的作业,可以在指定的时间点执行一次或在指定时间点执行多次任务. 一个Cron Job就好像Linux crontab中的一行,可以按照Cron定 ...

  8. c++ 在控制台用 wcout输出宽字符的问题

    在我的电脑上要想通过 std::wcout输出 宽字符 需加入以下代码 #include <io.h> #include <fcntl.h> void main() { _se ...

  9. hihocoder第226周:打表找规律

    题目列表 问题描述 有一个文本框,可以执行以下操作: 输入A Ctrl+C 复制 Ctrl+V 粘贴 Ctrl+A 全选 N次操作最多能够造出多少个A来? 输入一个N,输出一个整数,表示最多有多少个A ...

  10. 使用自连接、for xml path('')和stuff合并显示多行数据到一行中(转)

    原文: http://njm.iteye.com/blog/795881 --使用 自连接.for xml path('')和stuff合并显示多行数据到一行中 --注 --1.计算列可以不用包含在聚 ...