Home  >  Article  >  Backend Development  >  PHP引入命名空间 无法使用的问题 报错!

PHP引入命名空间 无法使用的问题 报错!

WBOY
WBOYOriginal
2016-06-06 20:44:092103browse

环境

使用的是ThinkPHP3.2.2,发现命名空间出现问题,无法使用!

文件

<code class="lang-php">#/Application/Common/Common/Api/WebServer.php
namespace Common\Api;
class WebServer {

    public function demo(){
        var_dump( __NAMESPACE__ );
        var_dump( __FILE__ , __LINE__ );            
    }
}

</code>
<code class="lang-php">#/Application/Home/Controller/InitController.class.php
namespace Home\Controller;
use Think\Controller;
use Common\Api;
class IndexController extend Controller {

    public function index(){
        //引入了 /Application/Common/Common/Api/WebServer.php
        load('Common.Api.WebServer');
    }
}
</code>
<code class="lang-php">#/Application/Home/Controller/IndexController.class.php
namespace Home\Controller;
class IndexController extend InitController {

    public function index(){
        $demo = new WebServer();
        var_dump( $demo->demo() );
    }
}
</code>

报错

<code>#访问 HomeController 控制器的 index模块报错提示:
Class 'Home\Controller\WebServer' not found
</code>

回复内容:

环境

使用的是ThinkPHP3.2.2,发现命名空间出现问题,无法使用!

文件

<code class="lang-php">#/Application/Common/Common/Api/WebServer.php
namespace Common\Api;
class WebServer {

    public function demo(){
        var_dump( __NAMESPACE__ );
        var_dump( __FILE__ , __LINE__ );            
    }
}

</code>
<code class="lang-php">#/Application/Home/Controller/InitController.class.php
namespace Home\Controller;
use Think\Controller;
use Common\Api;
class IndexController extend Controller {

    public function index(){
        //引入了 /Application/Common/Common/Api/WebServer.php
        load('Common.Api.WebServer');
    }
}
</code>
<code class="lang-php">#/Application/Home/Controller/IndexController.class.php
namespace Home\Controller;
class IndexController extend InitController {

    public function index(){
        $demo = new WebServer();
        var_dump( $demo->demo() );
    }
}
</code>

报错

<code>#访问 HomeController 控制器的 index模块报错提示:
Class 'Home\Controller\WebServer' not found
</code>

<code class="lang-php"><?php #/Application/Home/Controller/IndexController.class.php
use \Common\Api; 然后 new WebServer();
#或者
new \Common\Api\WebServer();
</code></code>
Statement:
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