请讲解一下这两句代码:
namespace Home\Controller;
use Think\Controller;
<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller{
}
请讲解一下这两句代码:
namespace Home\Controller;
use Think\Controller;
<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller{
}
1 回答932 阅读✓ 已解决
678 阅读
namespace 是定义这个文件的名字空间,当你
requie
或者include
这个文件想要使用UserController
这个类的时候,必须这么写:use 的作用是将该名字空间内的变量释放出来,如果你使用了如下代码
那么一下代码和第一句代码的效果是一样的:
题中代码的
use Think\Controller
是一个道理,就不多表了。使用名字空间的好处是能有效避免变量名冲突的问题,所以在使用use
前需要确保变量名释放后不会造成冲突覆盖。关于 namespace 的更多概念可查阅手册:http://php.net/manual/zh/language.namespaces.basics.php