PHP使用了命名空间后, 常见的几种导入方法:
1. names.php文件代码[这里导入了其它类]
namespace zhuangzi; header('Content-Type: text/html; charset=UTF-8'); class Test{ public function show(){ echo "庄子命名空间下的show函数"; } } $a = new Test(); // 实例化当前空间下的Test 类zhuangzi\Test(); $a->show(); include("abc/a/Haha.php"); $b = new \abc\a\Haha(); //完整访问 $b->show(); use abc\a as t; //为命名空间起个别名; $b = new t\Haha(); use abc\a\Haha as Ta; //为命名空间下的类起个别名 $b = new Ta(); use abc\a\Haha; // 等同于 use abc\a\Haha as Haha; $b = new Haha(); $b->show(); include("a.php"); $c = new \abc();//没有命名空间的类文件,相当于在公共空间2. Haha.php文件代码:
namespace abc\a; class Haha{ public function show(){ echo "man空间是的show函数"; } }3. a.php文件代码:
class abc{}