PHP命名空间

(1)定义命名空间:

<?php
namespace { }      //global          
namespace Foo\Bar\sub {}

(2)namespace的实现原理:相当于声明了一个名字是A\B的类

<?php
use Yaf as A;
$a = new A\Application();

(3) 所以下面的代码

<?php
//Fatal Error
use Foo\Bar\sub;
$a = new Application();

//OK
use Foo\Bar\sub;
$a = new Foo\Bar\sub\Application();

//OK
$a = new Foo\Bar\sub\Application();

(4)work with autoload

<?php
function __autoload($class) {
       $parts = explode('\\', $class);
       require end($parts) . '.php';
}
use Person\Barnes\David as MyPerson;
$class = new MyPerson\Class1();

(5)case-insensitive

(6)任意PHP代码都可以包含在命名空间中,但只有类、函数、常量受影响

(7)Namespace declaration statement has to be the very first statement in the script;
在声明命名空间之前唯一合法的代码是用于定义源文件编码方式的 declare 语句;
允许将同一个命名空间的内容分割存放在不同的文件中。

(8)注意访问任意全局类、函数或常量,都可以使用完全限定名称,例如 \strlen() 或 \Exception 或 \INI_ALL;最好使用完全限定名

<?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.

(9)命名空间和动态语言特征:必须使用完全限定名称。所以在动态的类名称、函数名称或常量名称中,限定名称和完全限定名称没有区别,因此其前导的反斜杠是不必要的。

<?php
$a = '\namespacename\classname';
$obj = new $a;
$a = 'namespacename\classname';
$obj = new $a;
echo constant('\namespacename\constname'), "\n";
echo constant('namespacename\constname'), "\n";       
$a = '\namespacename\classname';
$obj = new $a;
$a = 'namespacename\classname';
$obj = new $a;
echo constant('\namespacename\constname'), "\n";
echo constant('namespacename\constname'), "\n"; 

(10)When extending a class from another namespace that should instantiate a class from within the current namespace, you need to pass on the namespace.

<?php //File1.php
namespace foo;
class A {
   protected $namespace = __NAMESPACE__;
   public function factory() {
       $c = $this->namespace . '\C';
       return new $c;
   }
}
class C {
    public function tell() {
        echo "foo";
    }
}
<?php // File2.php
namespace bar;
class B extends \foo\A {
    protected $namespace = __NAMESPACE__;
}
class C {
    public function tell() {
        echo "bar";
    }
}
<?php
include "File1.php";
include "File2.php";
$b = new bar\B;
$c = $b->factory();
$c->tell(); // "bar"

(11)PHP支持两种抽象的访问当前命名空间内部元素的方法,NAMESPACE 魔术常量和namespace关键字。
在全局的,不包括在任何命名空间中的代码,它包含一个空的字符串;
关键字 namespace 可用来显式访问当前命名空间或子命名空间中的元素(namespace\xxx)。它等价于类中的 self 操作符。

(12)如果没有定义任何命名空间,所有的类和函数的定义都是在全局空间

(13)允许通过别名引用或导入外部的完全限定名称,前导的反斜线是不必要的

(14)You are allowed to “use” the same resource multiple times as long as it is imported under a different alias at each invocation.

<?php
use Lend\l1\Keller as Stellar;
use Lend\l1\Keller as Zellar;
use Lend\l2\Keller as Dellar;

(15)Because imports happen at compile time, there’s no polymorphism potential by embedding the use keyword in a conditonal. Actually, The “use” keyword can not be declared inside the function or method. It should be declared as global, after the “namespace”

<?php    //Fatal error
if ($objType == 'canine') {
  use Animal\Canine as Beast;
}
if ($objType == 'bovine') {
  use Animal\Bovine as Beast;
}

$oBeast = new Beast;
$oBeast->feed();
?>

(16)命名空间的定义,一定不能以反斜线开头

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值