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

WBOY
Release: 2016-06-06 20:44:09
Original
2108 people have browsed it

环境

使用的是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>
Copy after login
Copy after login
<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>
Copy after login
Copy after login
<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>
Copy after login
Copy after login

报错

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

回复内容:

环境

使用的是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>
Copy after login
Copy after login
<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>
Copy after login
Copy after login
<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>
Copy after login
Copy after login

报错

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

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