Home > Article > Backend Development > PHP引入命名空间 无法使用的问题 报错!
使用的是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>