锟斤拷锟狡斤拷锟斤拷锟斤拷锟斤拷

(PHP 5 >= 5.3.0, PHP 7)

锟斤拷说锟斤拷锟斤拷锟狡斤拷锟斤拷锟斤拷锟斤拷之前锟斤拷锟斤拷锟斤拷锟饺匡拷一些锟斤拷要锟侥讹拷锟藉:

锟斤拷锟斤拷锟秸硷拷锟斤拷锟狡讹拷锟斤拷
锟斤拷锟睫讹拷锟斤拷锟斤拷Unqualified name

锟斤拷锟斤拷锟叫诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟秸硷拷指锟斤拷锟斤拷谋锟绞讹拷锟斤拷锟斤拷锟斤拷锟� Foo

锟睫讹拷锟斤拷锟斤拷Qualified name

锟斤拷锟斤拷锟叫猴拷锟斤拷锟斤拷锟斤拷锟秸硷拷指锟斤拷锟斤拷谋锟绞讹拷锟斤拷锟斤拷锟斤拷锟� Foo\Bar

锟斤拷全锟睫讹拷锟斤拷锟斤拷Fully qualified name

锟斤拷锟斤拷锟叫帮拷锟斤拷锟斤拷锟斤拷锟秸硷拷指锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷占锟街革拷锟斤拷锟斤拷始锟侥憋拷识锟斤拷锟斤拷锟斤拷锟斤拷 \Foo\Bar锟斤拷 namespace\Foo 也锟斤拷一锟斤拷锟斤拷全锟睫讹拷锟斤拷锟狡★拷

锟斤拷锟狡斤拷锟斤拷锟斤拷循锟斤拷锟叫癸拷锟斤拷

  1. 锟斤拷锟斤拷全锟睫讹拷锟斤拷锟狡的猴拷锟斤拷锟斤拷锟斤拷统锟斤拷锟斤拷牡锟斤拷锟斤拷诒锟斤拷锟绞憋拷锟斤拷锟斤拷锟斤拷锟斤拷锟� new \A\B 锟斤拷锟斤拷为锟斤拷 A\B锟斤拷
  2. 锟斤拷锟叫的凤拷锟睫讹拷锟斤拷锟狡猴拷锟睫讹拷锟斤拷锟狡o拷锟斤拷锟斤拷全锟睫讹拷锟斤拷锟狡o拷锟斤拷锟捷碉拷前锟侥碉拷锟斤拷锟斤拷锟斤拷诒锟斤拷锟绞憋拷锟斤拷锟阶拷锟斤拷锟斤拷锟斤拷纾拷锟斤拷锟斤拷锟斤拷锟秸硷拷 A\B\C 锟斤拷锟斤拷锟斤拷为 C锟斤拷锟斤拷么锟斤拷 C\D\e() 锟侥碉拷锟矫就会被转锟斤拷为 A\B\C\D\e()锟斤拷
  3. 锟斤拷锟斤拷锟斤拷锟秸硷拷锟节诧拷锟斤拷锟斤拷锟叫碉拷没锟叫革拷锟捷碉拷锟斤拷锟斤拷锟阶拷锟斤拷锟斤拷薅锟斤拷锟斤拷凭锟斤拷锟斤拷锟斤拷锟角帮拷锟斤拷锟较碉拷前锟斤拷锟斤拷锟斤拷锟秸硷拷锟斤拷锟狡★拷锟斤拷锟界,锟斤拷锟斤拷锟斤拷锟秸硷拷 A\B 锟节诧拷锟斤拷锟斤拷 C\D\e()锟斤拷锟斤拷 C\D\e() 锟结被转锟斤拷为 A\B\C\D\e() 锟斤拷
  4. 锟斤拷锟睫讹拷锟斤拷锟斤拷锟斤拷锟捷碉拷前锟侥碉拷锟斤拷锟斤拷锟斤拷诒锟斤拷锟绞弊拷锟斤拷锟斤拷锟饺拷锟斤拷锟斤拷锟教的碉拷锟斤拷锟斤拷锟狡o拷锟斤拷锟斤拷锟界,锟斤拷锟斤拷锟斤拷锟斤拷占锟� A\B\C 锟斤拷锟斤拷为C锟斤拷锟斤拷 new C() 锟斤拷转锟斤拷为 new A\B\C() 锟斤拷
  5. 锟斤拷锟斤拷锟斤拷锟秸硷拷锟节诧拷锟斤拷锟斤拷锟斤拷A\B锟斤拷锟斤拷锟皆凤拷锟睫讹拷锟斤拷锟狡的猴拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷时锟斤拷锟斤拷锟侥★拷锟斤拷锟斤拷院锟斤拷锟� foo() 锟侥碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥o拷
    1. 锟节碉拷前锟斤拷锟斤拷锟秸硷拷锟叫诧拷锟斤拷锟斤拷为 A\B\foo() 锟侥猴拷锟斤拷
    2. 锟斤拷锟皆诧拷锟揭诧拷锟斤拷锟斤拷 全锟斤拷(global) 锟秸硷拷锟叫的猴拷锟斤拷 foo()锟斤拷
  6. 锟斤拷锟斤拷锟斤拷锟秸间(锟斤拷锟斤拷A\B锟斤拷锟节诧拷锟皆凤拷锟睫讹拷锟斤拷锟狡伙拷锟睫讹拷锟斤拷锟斤拷锟洁(锟斤拷锟斤拷全锟睫讹拷锟斤拷锟狡o拷锟侥碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷时锟斤拷锟斤拷锟侥★拷锟斤拷锟斤拷锟角碉拷锟斤拷 new C() 锟斤拷 new D\E() 锟侥斤拷锟斤拷锟斤拷锟教o拷 new C()锟侥斤拷锟斤拷:
    1. 锟节碉拷前锟斤拷锟斤拷锟秸硷拷锟叫诧拷锟斤拷A\B\C锟洁。
    2. 锟斤拷锟斤拷锟皆讹拷装锟斤拷锟斤拷A\B\C锟斤拷
    new D\E()锟侥斤拷锟斤拷:
    1. 锟斤拷锟斤拷锟斤拷锟斤拷前锟斤拷锟斤拷系锟角帮拷锟斤拷锟斤拷占锟斤拷锟斤拷票锟缴o拷A\B\D\E锟斤拷然锟斤拷锟斤拷腋锟斤拷唷�
    2. 锟斤拷锟斤拷锟皆讹拷装锟斤拷锟斤拷 A\B\D\E锟斤拷
    为锟斤拷锟斤拷锟斤拷全锟斤拷锟斤拷锟斤拷锟秸硷拷锟叫碉拷全锟斤拷锟洁,锟斤拷锟斤拷使锟斤拷锟斤拷全锟睫讹拷锟斤拷锟斤拷 new \C()锟斤拷

Example #1 锟斤拷锟狡斤拷锟斤拷示锟斤拷

<?php
namespace A;
use 
B\DC\as F;

// 锟斤拷锟斤拷锟斤拷锟斤拷

foo();      // 锟斤拷锟饺筹拷锟皆碉拷锟矫讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟秸硷拷"A"锟叫的猴拷锟斤拷foo()
            // 锟劫筹拷锟皆碉拷锟斤拷全锟街猴拷锟斤拷 "foo"

\foo();     // 锟斤拷锟斤拷全锟街空间函锟斤拷 "foo" 

my\foo();   // 锟斤拷锟矫讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟秸硷拷"A\my"锟叫猴拷锟斤拷 "foo" 

F();        // 锟斤拷锟饺筹拷锟皆碉拷锟矫讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟秸硷拷"A"锟叫的猴拷锟斤拷 "F" 
            // 锟劫筹拷锟皆碉拷锟斤拷全锟街猴拷锟斤拷 "F"

// 锟斤拷锟斤拷锟斤拷

new B();    // 锟斤拷锟斤拷锟斤拷锟斤拷锟秸硷拷 "A" 锟叫讹拷锟斤拷锟斤拷锟� "B" 锟斤拷一锟斤拷锟斤拷锟斤拷
            // 锟斤拷锟轿达拷业锟斤拷锟斤拷锟斤拷锟斤拷远锟阶帮拷锟斤拷锟� "A\B"

new D();    // 使锟矫碉拷锟斤拷锟斤拷颍锟斤拷锟斤拷锟斤拷锟斤拷占锟� "B" 锟叫讹拷锟斤拷锟斤拷锟� "D" 锟斤拷一锟斤拷锟斤拷锟斤拷
            // 锟斤拷锟轿达拷业锟斤拷锟斤拷锟斤拷锟斤拷远锟阶帮拷锟斤拷锟� "B\D"

new F();    // 使锟矫碉拷锟斤拷锟斤拷颍锟斤拷锟斤拷锟斤拷锟斤拷占锟� "C" 锟叫讹拷锟斤拷锟斤拷锟� "E" 锟斤拷一锟斤拷锟斤拷锟斤拷
            // 锟斤拷锟轿达拷业锟斤拷锟斤拷锟斤拷锟斤拷远锟阶帮拷锟斤拷锟� "C\E"

new \B();   // 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷全锟街空硷拷锟叫碉拷锟斤拷 "B" 锟斤拷一锟斤拷锟斤拷锟斤拷
            // 锟斤拷锟轿达拷锟斤拷郑锟斤拷锟斤拷锟斤拷远锟阶帮拷锟斤拷锟� "B"

new \D();   // 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷全锟街空硷拷锟叫碉拷锟斤拷 "D" 锟斤拷一锟斤拷锟斤拷锟斤拷
            // 锟斤拷锟轿达拷锟斤拷郑锟斤拷锟斤拷锟斤拷远锟阶帮拷锟斤拷锟� "D"

new \F();   // 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷全锟街空硷拷锟叫碉拷锟斤拷 "F" 锟斤拷一锟斤拷锟斤拷锟斤拷
            // 锟斤拷锟轿达拷锟斤拷郑锟斤拷锟斤拷锟斤拷远锟阶帮拷锟斤拷锟� "F"

// 锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟秸硷拷锟叫的撅拷态锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟秸间函锟斤拷

B\foo();    // 锟斤拷锟斤拷锟斤拷锟斤拷锟秸硷拷 "A\B" 锟叫猴拷锟斤拷 "foo"

B::foo();   // 锟斤拷锟斤拷锟斤拷锟斤拷锟秸硷拷 "A" 锟叫讹拷锟斤拷锟斤拷锟� "B" 锟斤拷 "foo" 锟斤拷锟斤拷
            // 锟斤拷锟轿达拷业锟斤拷锟� "A\B" 锟斤拷锟斤拷锟斤拷锟皆讹拷装锟斤拷锟斤拷 "A\B"

D::foo();   // 使锟矫碉拷锟斤拷锟斤拷颍锟斤拷锟斤拷锟斤拷锟斤拷占锟� "B" 锟叫讹拷锟斤拷锟斤拷锟� "D" 锟斤拷 "foo" 锟斤拷锟斤拷
            // 锟斤拷锟斤拷锟� "B\D" 未锟揭碉拷锟斤拷锟斤拷锟斤拷锟皆讹拷装锟斤拷锟斤拷 "B\D"

\B\foo();   // 锟斤拷锟斤拷锟斤拷锟斤拷锟秸硷拷 "B" 锟叫的猴拷锟斤拷 "foo" 

\B::foo();  // 锟斤拷锟斤拷全锟街空硷拷锟叫碉拷锟斤拷 "B" 锟斤拷 "foo" 锟斤拷锟斤拷
            // 锟斤拷锟斤拷锟� "B" 未锟揭碉拷锟斤拷锟斤拷锟斤拷锟皆讹拷装锟斤拷锟斤拷 "B"

// 锟斤拷前锟斤拷锟斤拷锟秸硷拷锟叫的撅拷态锟斤拷锟斤拷锟斤拷锟斤拷

A\B::foo();   // 锟斤拷锟斤拷锟斤拷锟斤拷锟秸硷拷 "A\A" 锟叫讹拷锟斤拷锟斤拷锟� "B" 锟斤拷 "foo" 锟斤拷锟斤拷
              // 锟斤拷锟斤拷锟� "A\A\B" 未锟揭碉拷锟斤拷锟斤拷锟斤拷锟皆讹拷装锟斤拷锟斤拷 "A\A\B"

\A\B::foo();  // 锟斤拷锟斤拷锟斤拷锟斤拷锟秸硷拷 "A\B" 锟叫讹拷锟斤拷锟斤拷锟� "B" 锟斤拷 "foo" 锟斤拷锟斤拷
              // 锟斤拷锟斤拷锟� "A\B" 未锟揭碉拷锟斤拷锟斤拷锟斤拷锟皆讹拷装锟斤拷锟斤拷 "A\B"
?>