Home > Backend Development > PHP Tutorial > PHP中命名空间的问题

PHP中命名空间的问题

WBOY
Release: 2016-06-06 20:27:19
Original
1259 people have browsed it

a.php下有个命名空间

<code>namespace foo;
class classA{
    public function foo_test(){
            echo "this is instance function";
        }  
}</code>
Copy after login
Copy after login

在factory.php下

<code>namespace factory;
import('a.php');
class classB{
    public function createA(){
        return \foo\classA::foo_test();
    }
}
</code>
Copy after login
Copy after login

为什么在factory.php下的createA方法会告诉我找不到 \foo\classA在factory.php下...它应该去a.php下找的啊,在php.5.5是可以通过的(找a.php),但是在5.6后就会找(factory.php)

请问这个如何解决啊。。。

回复内容:

a.php下有个命名空间

<code>namespace foo;
class classA{
    public function foo_test(){
            echo "this is instance function";
        }  
}</code>
Copy after login
Copy after login

在factory.php下

<code>namespace factory;
import('a.php');
class classB{
    public function createA(){
        return \foo\classA::foo_test();
    }
}
</code>
Copy after login
Copy after login

为什么在factory.php下的createA方法会告诉我找不到 \foo\classA在factory.php下...它应该去a.php下找的啊,在php.5.5是可以通过的(找a.php),但是在5.6后就会找(factory.php)

请问这个如何解决啊。。。

PHP中的引用有以下几种方式:

  1. require(file_path);

  2. require_once(file_path_;

  3. include(file_path);

  4. include_once(file_path);

  5. use 'file_path';
    具体区别自己去google

最后我想问,你这是写的什么东西???

php 好像真的没有 import() 这个内建方法...
按照 @lanffy 兄弟的改一下就好了...

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template