標籤:全域 ace port 包含 glob 操作符 別名 namespace 轉換
file1.php
<?phpnamespace Foo\Bar\subnamespace; const FOO = 1;function foo() {}class foo{ static function staticmethod() {}}?>
file2.php
<?phpnamespace Foo\Bar;include ‘file1.php‘;const FOO = 2;function foo() {}class foo{ static function staticmethod() {}}/* 非限定名稱 */foo(); // 解析為函數 Foo\Bar\foofoo::staticmethod(); // 解析為類 Foo\Bar\foo的靜態方法staticmethod。echo FOO; // resolves to constant Foo\Bar\FOO/* 限定名稱 */subnamespace\foo(); // 解析為函數 Foo\Bar\subnamespace\foosubnamespace\foo::staticmethod(); // 解析為類 Foo\Bar\subnamespace\foo, // 以及類的方法 staticmethodecho subnamespace\FOO; // 解析為常量 Foo\Bar\subnamespace\FOO /* 完全限定名稱 */\Foo\Bar\foo(); // 解析為函數 Foo\Bar\foo\Foo\Bar\foo::staticmethod(); // 解析為類 Foo\Bar\foo, 以及類的方法 staticmethodecho \Foo\Bar\FOO; // 解析為常量 Foo\Bar\FOO?>
在命名空間內部訪問全域類、函數和常量:
<?phpnamespace Foo;function strlen() {}const INI_ALL = 3;class Exception {}$a = \strlen(‘hi‘); // 調用全域函數strlen$b = \INI_ALL; // 訪問全域常量 INI_ALL$c = new \Exception(‘error‘); // 執行個體化全域類 Exception?>
命名空間和動態語言特徵
PHP 命名空間的實現受到其語言自身的動態特徵的影響。因此,如果要將下面的代碼轉換到命名空間中,動態訪問元素。
//example.php<?phpclass classname{ function __construct() { echo __METHOD__,"\n"; }}function funcname(){ echo __FUNCTION__,"\n";}const constname = "global";$a = ‘classname‘;$obj = new $a; // prints classname::__construct$b = ‘funcname‘;$b(); // prints funcnameecho constant(‘constname‘), "\n"; // prints global?>
<?phpnamespace namespacename;class classname{ function __construct() { echo __METHOD__,"\n"; }}function funcname(){ echo __FUNCTION__,"\n";}const constname = "namespaced";include ‘example1.php‘;//全域命名空間和某個命名空間有相同類 函數 和 常量 分別區別訪問$a = ‘classname‘;$obj = new $a; // prints classname::__construct$b = ‘funcname‘;$b(); // prints funcnameecho constant(‘constname‘), "\n"; // prints global/* note that if using double quotes, "\\namespacename\\classname" must be used */$a = ‘\namespacename\classname‘;$obj = new $a; // prints namespacename\classname::__construct$a = ‘namespacename\classname‘;$obj = new $a; // also prints namespacename\classname::__construct$b = ‘namespacename\funcname‘;$b(); // prints namespacename\funcname$b = ‘\namespacename\funcname‘;$b(); // also prints namespacename\funcnameecho constant(‘\namespacename\constname‘), "\n"; // prints namespacedecho constant(‘namespacename\constname‘), "\n"; // also prints namespaced?>namespace關鍵字和__NAMESPACE__常量
<?phpnamespace MyProject;echo ‘"‘, __NAMESPACE__, ‘"‘; // 輸出 "MyProject"?>
<?phpecho ‘"‘, __NAMESPACE__, ‘"‘; // 輸出 ""?>
使用__NAMESPACE__動態建立名稱
<?phpnamespace MyProject;function get($classname){ $a = __NAMESPACE__ . ‘\\‘ . $classname; return new $a;}?>
關鍵字 namespace 可用來顯式訪問當前命名空間或子命名空間中的元素。它等價於類中的 self 操作符。
<?phpnamespace MyProject;use blah\blah as mine; // see "Using namespaces: importing/aliasing"blah\mine(); // calls function blah\blah\mine()namespace\blah\mine(); // calls function MyProject\blah\mine()namespace\func(); // calls function MyProject\func()namespace\sub\func(); // calls function MyProject\sub\func()namespace\cname::method(); // calls static method "method" of class MyProject\cname$a = new namespace\sub\cname(); // instantiates object of class MyProject\sub\cname$b = namespace\CONSTANT; // assigns value of constant MyProject\CONSTANT to $b?>
在PHP中,別名是通過操作符 use 來實現的. 下面是一個使用所有可能的三種匯入方式的例子:
- 使用use操作符匯入/使用別名
<?phpnamespace foo;use My\Full\Classname as Another;// 下面的例子與 use My\Full\NSname as NSname 相同use My\Full\NSname;// 匯入一個全域類use \ArrayObject;$obj = new namespace\Another; // 執行個體化 foo\Another 對象$obj = new Another; // 執行個體化 My\Full\Classname 對象NSname\subns\func(); // 調用函數 My\Full\NSname\subns\func$a = new ArrayObject(array(1)); // 執行個體化 ArrayObject 對象// 如果不使用 "use \ArrayObject" ,則執行個體化一個 foo\ArrayObject 對象?>
- 一行中包含多個use語句
<?phpuse My\Full\Classname as Another, My\Full\NSname;$obj = new Another; // 執行個體化 My\Full\Classname 對象NSname\subns\func(); // 調用函數 My\Full\NSname\subns\func?><p>匯入操作是在編譯執行的,但動態類名稱、函數名稱或常量名稱則不是。</p><ol><li>匯入和動態名稱</li></ol><pre><?phpuse My\Full\Classname as Another, My\Full\NSname;$obj = new Another; // 執行個體化一個 My\Full\Classname 對象$a = ‘Another‘;$obj = new $a; // 實際化一個 Another 對象?>
- 匯入和動態名稱
<?phpuse My\Full\Classname as Another, My\Full\NSname;$obj = new Another; // 執行個體化一個 My\Full\Classname 對象$a = ‘Another‘;$obj = new $a; // 實際化一個 Another 對象?>
另外,匯入操作隻影響非限定名稱和限定名稱。完全限定名稱由於是確定的,故不受匯入的影響。
- 匯入和完全限定名稱
<?phpuse My\Full\Classname as Another, My\Full\NSname;$obj = new Another; // instantiates object of class My\Full\Classname$obj = new \Another; // instantiates object of class Another$obj = new Another\thing; // instantiates object of class My\Full\Classname\thing$obj = new \Another\thing; // instantiates object of class Another\thing?>
使用命名空間:後備全域函數/常量
在一個命名空間中,當 PHP 遇到一個非限定的類、函數或常量名稱時,它使用不同的優先策略來解析該名稱。
- 類名稱總是解析到當前命名空間中的名稱(找不到就報錯咯) 。因此在訪問系統內部或不包含在命名空間中的類名稱時,必須使用完全限定名稱
- 函數和常量來說,如果當前命名空間中不存在該函數或常量,PHP 會退而使用全域空間中的函數或常量。
<?phpnamespace 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 引入命名空間概念前一樣。在名稱前加上首碼 \ 表示該名稱是全域空間中的名稱,即使該名稱位於其它的命名空間中時也是如此。
<?phpnamespace A\B\C;/* 這個函數是 A\B\C\fopen */function fopen() { /* ... */ $f = \fopen(...); // 調用全域的fopen函數 return $f;} ?>
PHP知識點總結4