You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.1 KiB

3 years ago
  1. <?php
  2. //命名空间a
  3. namespace a
  4. {
  5. const const_a=1; //常量a
  6. function func_a()
  7. {
  8. echo __FUNCTION__.'<br>';
  9. }
  10. class cls_a
  11. {
  12. static function func()
  13. {
  14. echo __CLASS__.'--'.__FUNCTION__.'<br>';
  15. }
  16. }
  17. }
  18. //命名空间b
  19. namespace b
  20. {
  21. //导入类
  22. use a\cls_a as cls_b; //导入命名空间a\b\c的类cls_a,设置别名cls_a
  23. $obj=new cls_b;
  24. $obj -> func();
  25. //导入函数
  26. use function a\func_a;
  27. func_a();
  28. //导入常量
  29. use const a\const_a;
  30. echo const_a.'<br>';
  31. }
  32. namespace c
  33. {
  34. //导入命名空间
  35. use a as aa;
  36. echo aa\const_a.'<br>';;
  37. aa\func_a();
  38. $obj=new aa\cls_a();
  39. $obj -> func();
  40. }
  41. //感悟:命名空间就像在一个文件中把其中一筐代码框住,放在一个空间中,当要使用是就用use导入
  42. //注意:导入不同文件的命名空间需要用iclude'file'
  43. ?>