php - 2 namespace with same class name - is it need to use "use" alias to call first namespace? -


from below code, try call sayhi method, in class cat namespace foo; not work, have use "use" change name 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 use backslash path  fatal error: class 'bar\foo\cat' not found  ?> 

if use cat::sayhi(); call method namespace bar don't want. question: there method solve problem without use 'use' alias?

when declare namespace, code operates within namespace until declare another, or until end of file. you're doing this:

namespace foo; class cat {} namespace bar; class cat {} // here in namespace bar foo\cat::sayhi(); 

since you're in bar namespace, , namespace reference doesn't anchor root namespace, final line interpreted as:

\bar\foo\cat::sayhi(); 

simply anchor root:

\foo\cat::sayhi(); 

or put call foo\cat::sayhi(); in file.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -