PHP ??????
PHP ??????(??????)? PHP 5.3?? ???????. C#? Java? ????? ??????? ??? ?? ????. ??? ?? PHP?? ??? ?? ??? ??? ????.
PHP ??????? ?? ? ?? ??? ??? ??? ? ????.
???? ??? ??? PHP ?? ???/??/?? ?? ?? ???/??/ ?? ?? ??.
?? ? ??? ??(????? ? ?? ??? ??? ???? ?? ???)? ?? ??(?? ??) ??? ??? ?? ??? ???? ????.
?????? ??
????? ?? ??, ??? ? ?? ??? PHP? ??????? ???? ?? ????? ?? ??? ?????.
???? ??? ??? ? ????? ??, ??, ??? ???? ??? ???? ?????. ? ??? ????? ?? ??? ??? ? ????. ??????? ??? ?????? ??? ?????. ??? ??????? ???? ??? ?? ?? ???? ?? ??????? ???? ???. ?? ??? ??? ????.
<?php// ??? 'MyProject' ??????
MyProject;? ???? ????. .. code..
?>
??? ?? ??? ??? ?? ?? ?????? ??? ??? ?? ????.
< ?php
namespace MyProject1;// MyProject1 ??????? PHP ??
namespace MyProject2
// MyProject2 ??????? PHP ??
// ? ?? ??
namespace MyProject3 {
// MyProject3 ??????? PHP ??
}
?>
??????? ???? ?? ??? ?? ??? ???? ???? ??????. ??? ???? PHP? ?? ??? ?????? ?? ?? ??? ? ????. <?php
declare(encoding='UTF-8'); //定義多個(gè)命名空間和不包含在命名空間中的代碼
namespace MyProject {
const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */ }
}
namespace { // 全局代碼
session_start();
$a = MyProject\connect();
echo MyProject\Connection::start();
}
?>
?? ???? ?? ??? ????.
<?php
namespace MyProject; ?????? html>"? ???? ??? ??????. ??????? ???? ?????>
?? ??????
? ? ?? ???? ???. ?? ?????? ??? ??? ????. ?? ?? ?????. ??? ???? ??????? ?? ??????? ???? ?? ??? ????? ????. ?? ??????? ???? ???? ??? ? ?? ? ?? ????? ?? ? ????.
<?php
namespace MyProjectSubLevel; //??? ?? ?????? ??
const CONNECT_OK = 1;
class Connection { /* ... */ }function Connect() { /* .. . */ }
}
?>
?? ???? MyProjectSubLevelCONNECT_OK ??, MyProjectSubLevelConnection ??? ? MyProjectSubLevelConnect ??? ????.
?????? ??
PHP ??????? ??? ??? ? ?? ???? ??? ? ????.
????? ?? ?? ?? ???? ?? ??? ??(?: $a=new foo(); ?? foo::staticmethod();) ?? ??????? currentnamespace?? foo? currentnamespacefoo? ?????. foo? ???? ??? ?? ???? ?? ???????? ??? ???? ?? ?? foo? foo? ?????. ??: ??????? ?? ?? ??? ???? ?? ?? ????? ?? ?? ?? ?? ??? ?? ?? ?? ?? ???? ?????.
???? ?? ?? ???? ??? ??(?: $a = new subnamespacefoo(); ?? subnamespacefoo::staticmethod();) ?? ??????? currentnamespace?? foo? currentnamespacesubnamespacefoo? ?????. foo? ???? ??? ?? ??? ??, ?? ???????? ???? ?? ??? foo? subnamespacefoo? ?????.
???? ?? ?? ?? ??? ???? ??? ??(?: $a = new currentnamespacefoo(); ?? currentnamespacefoo::staticmethod();). ? ?? foo? ?? ???? ??? ?? currentnamespacefoo? ?????.
??? ? ? ?? ??? ??? ????.
file1.php ?? ??
<?php namespace Foo\Bar\subnamespace; const FOO = 1; function foo() {} class foo { static function staticmethod() {} } ?>
file2.php ?? ??
<?php namespace Foo\Bar; include 'file1.php'; const FOO = 2; function foo() {} class foo { static function staticmethod() {} } /* 非限定名稱 */ foo(); // 解析為 Foo\Bar\foo resolves to function Foo\Bar\foo foo::staticmethod(); // 解析為類 Foo\Bar\foo的靜態(tài)方法staticmethod。resolves to class Foo\Bar\foo, method staticmethod echo FOO; // resolves to constant Foo\Bar\FOO /* 限定名稱 */ subnamespace\foo(); // 解析為函數(shù) Foo\Bar\subnamespace\foo subnamespace\foo::staticmethod(); // 解析為類 Foo\Bar\subnamespace\foo, // 以及類的方法 staticmethod echo subnamespace\FOO; // 解析為常量 Foo\Bar\subnamespace\FOO /* 完全限定名稱 */ \Foo\Bar\foo(); // 解析為函數(shù) Foo\Bar\foo \Foo\Bar\foo::staticmethod(); // 解析為類 Foo\Bar\foo, 以及類的方法 staticmethod echo \Foo\Bar\FOO; // 解析為常量 Foo\Bar\FOO ?>
????? ?? ???, ?? ?? ??? ?????? strlen(), Exception ?? INI_ALL? ?? ???? ??? ??? ? ????.
?????? ?? ?? ???, ?? ? ??? ???:
<?php namespace Foo; function strlen() {} const INI_ALL = 3; class Exception {} $a = \strlen('hi'); // 調(diào)用全局函數(shù)strlen $b = \INI_ALL; // 訪問(wèn)全局常量 INI_ALL $c = new \Exception('error'); // 實(shí)例化全局類 Exception ?>
?????? ? ?? ?? ??
PHP ?????? ??? ??? ??? ????. ?? ??? ???? ??? ??. ??? ?? ??? ??????? ????? ?? ??? ???? ??????.
example1.php ?? ??:
<?php class 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 funcname echo constant('constname'), "\n"; // prints global ?>
???? ??(?????? ???? ??? ??? ??)? ???? ???. ?? ??? ??, ?? ?? ?? ?? ????? ???? ??? ???? ?? ??? ??? ???? ?? ????? ???? ????.
?????? ??? ?? ?? ???
<?php namespace 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 funcname echo 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\funcname echo constant('\namespacename\constname'), "\n"; // prints namespaced echo constant('namespacename\constname'), "\n"; // also prints namespaced ?>
?????? ??? ? __NAMESPACE__ ??
PHP? ?? ?????? ?? ???? ??? ????? ?? ? ?? ???? ?????. __NAMESPACE__ ?? ?? ? ?????? ???.
?? __NAMESPACE__? ?? ?? ??????? ??? ???? ??????. ??????? ???? ?? ?? ???? ? ???? ?????.
__NAMESPACE__ ?, ??????? ??
<?php
namespace MyProject;
echo '"' , __NAMESPACE__, '"'; // "MyProject" ??
?>
__NAMESPACE__ ??, ?? ??
<?php
echo '"', __NAMESPACE__, '"'; // "" ??
?>
?? __NAMESPACE__? ??? ???? ??? ? ?????. ?:
__NAMESPACE__? ???? ???? ?? ???
<?php
?????? MyProject;
function get($classname)
{
$a = __NAMESPACE__
return new $a
}
?>
??? ??????? ???? ?? ?????? ?? ?? ??????? ??? ????? ???? ? ????. ???? self ???? ?????.
?????? ???, ??????? ??
<?php namespace 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 namespace\func(); // calls function func() namespace\sub\func(); // calls function sub\func() namespace\cname::method(); // calls static method "method" of class cname $a = new namespace\sub\cname(); // instantiates object of class sub\cname $b = namespace\CONSTANT; // assigns value of constant CONSTANT to $b ?>
?????? ??: alias/import
PHP ??????? ?? ?? ????? ???? ? ?? ??, ? ??? ??? ??? ????? ?????? ??? ??? ???? ??? ?????. PHP? ??? ??? ??? ???? ????.
PHP?? ??? ??? ??? ?? ?????. ??? ??? ? ?? ???? ??? ?? ???? ????.
1 ??? ????? ????? ?? ???? ?????.
<?php namespace foo; use My\Full\Classname as Another; // 下面的例子與 use My\Full\NSname as NSname 相同 use My\Full\NSname; // 導(dǎo)入一個(gè)全局類 use \ArrayObject; $obj = new namespace\Another; // 實(shí)例化 foo\Another 對(duì)象 $obj = new Another; // 實(shí)例化 My\Full\Classname 對(duì)象 NSname\subns\func(); // 調(diào)用函數(shù) My\Full\NSname\subns\func $a = new ArrayObject(array(1)); // 實(shí)例化 ArrayObject 對(duì)象 // 如果不使用 "use \ArrayObject" ,則實(shí)例化一個(gè) foo\ArrayObject 對(duì)象 ?>2. ? ?? ?? ?? use ?? ???? ????.
<?php use My\Full\Classname as Another, My\Full\NSname; $obj = new Another; // 實(shí)例化 My\Full\Classname 對(duì)象 NSname\subns\func(); // 調(diào)用函數(shù) My\Full\NSname\subns\func ?>??? ?? ???? ??? ????? ?? ??? ??, ?? ?? ?? ?? ??? ???? ????. 3. ???? ? ?? ??
<?php use My\Full\Classname as Another, My\Full\NSname; $obj = new Another; // 實(shí)例化一個(gè) My\Full\Classname 對(duì)象 $a = 'Another'; $obj = new $a; // 實(shí)際化一個(gè) Another 對(duì)象 ?>?? ???? ??? ????? ?? ??? ???? ???? ??? ????. ???? ??? ?????? ????? ??? ?? ????. 4. ???? ? ???? ??
<?php use My\Full\Classname as Another, My\Full\NSname; $obj = new Another; // 實(shí)例化對(duì)象的類 My\Full\Classname $obj = new \Another; // instantiates object of class Another $obj = new Another\thing; // 實(shí)例化對(duì)象的類 My\Full\Classname\thing $obj = new \Another\thing; // instantiates object of class Another\thing ?>?? ??? ???: ? PHP ??? ? ?? ??? ???. ?, PHP < ?php ?? ?????? ?? ???? ??? ???? ?? use ???? ???. use ???? ???? ??? ??? ? PHP? ??? ??????? ???? ??? ????? ??? ?? ?? ??? ??? ??? ??? ????. use ???? ?? ???? ???? ???. ?, ???? ????? ??? ? ????. use ???? ?????? ?? ??? ??? ? ????. ?? ??????? ??.
?????? ??: ?? ?? ??/??
???????? PHP? ????? ?? ???, ?? ?? ?? ??? ???? ?? ?? ?? ??? ???? ??? ?????. ??? ?????. ??? ??? ?? ?? ??????? ???? ?????. ??? ??? ?? ?? ??????? ???? ?? ??? ??? ????? ?? ??? ?? ???? ??? ???? ???. 1. ??????<?php namespace A\B\C; class Exception extends \Exception {} $a = new Exception('hi'); // $a 是類 A\B\C\Exception 的一個(gè)對(duì)象 $b = new \Exception('hi'); // $b 是類 Exception 的一個(gè)對(duì)象 $c = new ArrayObject; // 致命錯(cuò)誤, 找不到 A\B\C\ArrayObject 類 ?>? ?? ???? ??????. ??? ?? ??? ??? ?? ??????? ???? ??? PHP? ?? ??? ??? ??? ???? ???. 2. ??????? ?? ??/?? ??
<?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"; } ?>?? ????????? ???? ??? ?? ???? ??? ????? ?????. PHP? ?????? ??? ???? ??????. ?? ???? ??? ?? ??????? ???? ??? ?? ??? ??? ?????. ?? ?? ?? ???? ??????? ????? ??? ?? ?? ??? ???? ???. ?: throw new Exception();
<?php namespace A\B\C; /* 這個(gè)函數(shù)是 A\B\C\fopen */ function fopen() { /* ... */ $f = \fopen(...); // 調(diào)用全局的fopen函數(shù) return $f; ?>?????? ??
?????? ?? ?? ?? ??? ???? ?? ?? ???? ??? ? ? ???? ?? ?? ??? ???????.
<?php
?????? A;
BD, CE? F? ??
// ?? ??
foo(); / ?? ?????? "A"? ??? foo() ??? ??? ???.
// ?? ?? ?? ?? "foo"? ??? ???.
foo() // ?? ?? ?? ?? " foo"
myfoo(); // "Amy" ??????? ??? "foo" ?? ??
F(); // ?? "Amy" ??????? ??? ?? ??? ?????. A" F"
// ?? ?? "F"? ?? ??? ???.
// ??? ??
new B() // ??? ??????? ????. ?????? "A" ??? "B"? ??
// ?? ? ?? ?? ??? "AB" ?? ??? ?????.
new D() // ???? ??? ???? ??????? ??? ??? ?????. "B" ??? "D"? ??
// ?? ? ?? ?? ??? "BD" ?? ??? ?????.
new F() // ???? ??? ???? ??????? ??? ??? ?????. C" ??? "E"? ??
// ?? ? ??? ??? "CE" ?? ??? ?????.
new B(); // ?? ??? ??? ??? "B"? ??? ?????. space Object
// ?? ? ??? ??? "B"? ???? ?????.
new D() // ?? ??? ??? ??? "D"? ??? ????.
// ?? ? ??? ??? "D"? ???? ?????.
new F(); // ?? ??? ??? ??? "F"? ??? ????.
// ?? ? ??? ??? ?? ??? ?????. "F"
// ?? ???????? ?? ??? ?? ?????? ?? ??
Bfoo() // "AB" ???????? "foo" ?? ??
B ::foo(); // "A" ??????? ??? "B" ???? "foo" ???? ?????.
// "AB" ???? ??? ???? ??? ?????. "AB" ???
D::foo(); // ???? ??? ???? ?????? "B"? ??? ??? "D"? "foo" ???? ?????.
// ??? "BD"? ??? ??? ?????. "BD" ???? ???? ?????.
Bfoo(); // "B" ???????? "foo" ??? ?????.
B::foo(); ?? ???? "B" ???? " ???
// "B" ???? ?? ? ??? "B" ???? ???? ????? ???.
// ?? ???????? ?? ??? ?? ??
AB::foo(); // "AA" ??????? ??? "B" ???? "foo" ???? ?????.
// "AAB" ???? ??? "AAB" ???? ?? ??? ???. AAB"
AB::foo(); // "AB" ??????? ??? "B" ???? "foo" ??? ??
// "AB" ???? ??? ???? "AB" ??? ??? ?????.
?>
?? ??? ?? ??? ????.
1. ???? ??? ?? ??, ??? ? ??? ?? ??? ??? ??? ?????. ?? ?? ? AB? ??? AB? ?????.
2. ?? ????? ?? ??? ???? ??(????? ?? ??)? ?? ???? ??? ?? ??? ??? ?????. ?? ?? ?????? ABC? C? ??? ?? CDe()? ?? ??? ABCDe()? ?????.
3. ?????? ??? ???? ??? ?? ???? ?? ?? ???? ?? ??? ?? ?????? ??? ???. ?? ??, CDe()? ?????? AB ??? ???? CDe()? ABCDe()? ?????.
4. ????? ?? ??? ??? ?? ???? ??? ?? ??? ??? ?????(?? ???? ?? ?? ?? ??? ???). ?? ?? ?????? ABC? C? ??? ?? new C()? new ABC()? ?????.
5. ??????(?: AB) ??? ????? ?? ??? ?? ?? ??? ???? ?????. ?? ?? foo() ??? ?? ??? ??? ?? ?? ?????.
?? ???????? ABfoo()?? ??? ????.
?? ?? foo?? ??? ?? ??? ???. ().
6. ??????(?: AB) ??? ????? ?? ?? ?? ???? ?? ???(??? ????? ?? ??)? ?? ??? ???? ?????. new C()? new DE()? ???? ?? ??? ??? ??. new C() ??: new DE() ??: ??? ???????? ??? ???? ???? ?? ???? ??? new C()? ???? ???.
??? ?? ?? ?? ?????? ??? ???? ABDE? ??? ? ?? ???? ?????.
ABDE ???? ?? ??? ???.
?? ???????? ABC ???? ????.
ABC ???? ?? ??? ???.