drno94939847
drno94939847
2017-04-14 14:46

2具有相同类名的命名空间 - 是否需要使用“use”别名来调用第一个命名空间?

  • oop
  • php

From my below code, I try to call the sayHi method, in class Cat with namespace foo; but it not work, I have to use "use" to change name to call it;

    <?php

    namespace foo;

    class Cat
    {
        public static function sayHi()
        {
            echo "Meow";
        }
    }

    namespace bar;

    class Cat
    {
        public static function sayHi()
        {
            echo "Hello";
        }
    }


    foo\Cat::sayHi();  //try to use backslash path  but Fatal error: Class 'bar\foo\Cat' not found

?>

if I use Cat::sayHi(); it always call the method from namespace bar that I don't want. so Question: Are there any method that solve this problem without use 'use' alias?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答