TShopping

 找回密碼
 註冊
搜索
查看: 281|回復: 0
打印 上一主題 下一主題

[教學] php – namespace 命名空間教學介紹

[複製鏈接]
跳轉到指定樓層
1#
發表於 2023-5-12 21:48:25 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook

很多個 函式(function) 可以使用 類別(class) 包裝起來;
很多個 類別(class) 可以使用 命名空間(namespace) 包裝起來。

命名空間,可以解決不同人使用同樣名稱的類別,所造成的衝突。例如小明寫了一支取名hello的類別;小華也寫了一支取名hello的類別,一旦同時載入小明寫的跟小華寫的,若不使用命名空間的寫法,就會造成衝突。所以比較建議,命名空間是針對一個檔案使用一個命名空間;後面也舉例一個檔案多個命名空間的寫法。

a.php
  1. <?
  2. //小明取名一個叫做 a 的空間,在 namespace 後面的 class 都將會被歸類在 a 底下
  3. namespace a;

  4. class hello
  5. {
  6.     static public function get()
  7.     {
  8.         return "a get";
  9.     }
  10. }

  11. class byebye
  12. {
  13.     static public function get()
  14.     {
  15.         return "a byebye";
  16.     }
  17. }
複製代碼



b.php
  1. <?
  2. //小華取名一個叫做 b 的空間,在 namespace 後面的 class 都將會被歸類在 b 底下
  3. namespace b;

  4. class hello
  5. {
  6.     static public function get()
  7.     {
  8.         return "a get";
  9.     }
  10. }

  11. class byebye
  12. {
  13.     static public function get()
  14.     {
  15.         return "a byebye";
  16.     }
  17. }
複製代碼

開發者寫的 index.php
  1. <?

  2. //載入小明與小華寫的檔案,因為使用了命名空間,所以同樣的 class 名稱不會造成衝突
  3. include_once("a.php");
  4. include_once("b.php");

  5. echo a\hello::get(); //輸出 a get 這是小明寫的
  6. echo b\hello::get(); //輸出 b get 這是小華寫的


  7. echo a\byebye::get(); //輸出 c byebye 這是小明寫的
  8. echo b\byebye::get(); //輸出 d byebye 這是小華寫的
複製代碼


接著是使用 use 來處理的複雜分類方式...

a.php
  1. <?
  2. //小明取名一個叫做 a 的空間,在 namespace 後面的 class 都將會被歸類在 a 底下
  3. namespace a\man;

  4. class hello
  5. {
  6.     static public function get()
  7.     {
  8.         return "a\man hello::get()";
  9.     }
  10. }

  11. class byebye
  12. {
  13.     static public function get()
  14.     {
  15.         return "a\man byebye::get()";
  16.     }
  17. }

  18. namespace a\woman;

  19. class hello
  20. {
  21.     static public function get()
  22.     {
  23.         return "a\woman hello::get()";
  24.     }
  25. }

  26. class byebye
  27. {
  28.     static public function get()
  29.     {
  30.         return "a\woman byebye::get()";
  31.     }
  32. }
複製代碼

b.php
  1. <?
  2. //小華取名一個叫做 b 的空間,在 namespace 後面的 class 都將會被歸類在 b 底下
  3. namespace b\man;

  4. class hello
  5. {
  6.     static public function get()
  7.     {
  8.         return "b\man hello::get()";
  9.     }
  10. }

  11. class byebye
  12. {
  13.     static public function get()
  14.     {
  15.         return "b\man byebye::get()";
  16.     }
  17. }

  18. namespace b\woman;

  19. class hello
  20. {
  21.     static public function get()
  22.     {
  23.         return "b\woman hello::get()";
  24.     }
  25. }

  26. class byebye
  27. {
  28.     static public function get()
  29.     {
  30.         return "b\woman byebye::get()";
  31.     }
  32. }
複製代碼

index.php
  1. <?

  2. // 載入小明與小華寫的檔案,因為使用了命名空間,所以同樣的 class 名稱不會造成衝突
  3. include_once("a.php");
  4. include_once("b.php");

  5. //一、
  6. // 使用 a\man 並取別名為 a_man。當然也可以不使用 as 來取別名。
  7. use a\man as a_man;

  8. // 使用別名叫做 a_man 底下的類別 hello
  9. echo a_man\hello::get(); // 輸出 a\man hello::get()
  10. echo "<br>";

  11. echo a_man\byebye::get(); // 輸出 a\man byebye::get()
  12. echo "<br>";

  13. // 二、
  14. use a\woman as a_wonan;
  15. echo a_wonan\hello::get(); // 輸出 a\woman hello::get()
  16. echo "<br>";

  17. // 三、
  18. use b\man as b_man;
  19. echo b_man\hello::get(); // 輸出 b\man hello::get()
  20. echo "<br>";

  21. // 四、
  22. use b\woman as b_woman;
  23. echo b_woman\hello::get(); // 輸出 b\woman hello::get()
  24. echo "<br>";
複製代碼

參考文章
https://kiiuo.com/archives/1947/1947/

 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-30 08:51 , Processed in 0.064530 second(s), 23 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表