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.

68 lines
1.3 KiB

3 years ago
  1. <?php
  2. namespace Jack
  3. {
  4. $varA=__NAMESPACE__;
  5. const constA="这个一个常量";
  6. function fnA(){
  7. echo __NAMESPACE__."中的函数".__FUNCTION__.PHP_EOL;
  8. }
  9. class ClassA{
  10. public $varA=2;
  11. public function fnA(){
  12. echo __NAMESPACE__.__FUNCTION__.PHP_EOL;
  13. }
  14. }
  15. echo $varA.PHP_EOL;
  16. fnA();
  17. $obj=new ClassA;
  18. $obj->fnA();
  19. }
  20. namespace Tom
  21. {
  22. $varA=__NAMESPACE__;
  23. const content="这个一个常量";
  24. function fnA(){
  25. echo __NAMESPACE__.__FUNCTION__.PHP_EOL;
  26. }
  27. class ClassA{
  28. public $varA=2;
  29. public function fnA(){
  30. echo __NAMESPACE__.__FUNCTION__.PHP_EOL;
  31. }
  32. }
  33. echo $varA.PHP_EOL;
  34. fnA();
  35. $obj=new ClassA;
  36. $obj->fnA();
  37. }
  38. //全局代码
  39. namespace{
  40. $varA="全局代码中的变量";
  41. echo $varA.PHP_EOL;
  42. //调用其中一个命名
  43. // echo Jack\$varA.PHP_EOL; //变量不在命名空间的作用范围内
  44. echo Jack\constA; //常量
  45. Jack\fnA(); //函数
  46. $objA=new Jack\ClassA; //类
  47. echo $objA->varA.PHP_EOL;
  48. //echo Tom\$varA.PHP_EOL;
  49. }
  50. ?>