namespace namespaces
ThinkPHP5
Namespaces are used to define and automatically load class library files, which effectively solves the problem of namespace collisions between Multi-modules and Composer
class libraries, and implements a more efficient automatic loading mechanism for class Libraries.
If you don't know the basic concepts of namespaces, you can refer to the PHP manual: PHP namespaces
It is important to note that if you need to call Php's built-in class library, or if a third party does not have a class library that uses a namespace, remember to add it when instantiating the class library, \
for Example:
// 错误的用法$class = new stdClass();$xml = new SimpleXmlElement($xmlstr);// 正确的用法$class = new \stdClass();$xml = new \SimpleXmlElement($xmlstr);
In thinkphp 5.0
, you only need to properly define the namespace in which the class library is located, and the path to the namespace is consistent with the directory of the class library file, so you can implement the automatic loading of the class for true lazy Loading.
For example, \think\cache\driver\File
A class is defined As:
namespace think\cache\driver;class File {}
If we instantiate this class, it should be:
$class = new \think\cache\driver\File();
The system automatically loads the class file for the corresponding path of the class, where the path is thinkphp/library/think/cache/driver/File.php
.
5.0 The default directory specification is lowercase, the class file name is the hump method, and the first letter is Capitalized.
In principle, you can support a directory named after the hump, as long as the namespace definition is consistent with the directory, for example:
We instantiate
$class = new \Think\Cache\Driver\File();
The system will automatically load the thinkphp/library/Think/Cache/Driver/File.php
file.
Root namespace (class Library Package)
The root namespace is a key concept, taking the above \think\cache\driver\File
class as an example, think
is a root namespace, the corresponding initial namespace directory is the System's class Library Directory ( thinkphp/library/think
), We can simply understand a root namespace corresponding to a class library Package.
Several root namespaces (class library packages) built into the system are as Follows:
name |
Description |
Class Library Directory |
Think |
System Core Class Library |
Thinkphp/library/think |
Traits |
System Trait Class Library |
Thinkphp/library/traits |
App |
Application Class Library |
Application |
If you need to add a new root namespace, there are two ways to register a new root namespace or place a EXTEND_PATH
directory (autoenrollment).
Note that the sample code in this manual is meant for brevity, such as the absence of a namespace for the specified class library, meaning a think
namespace, such as the following code:
Route::get(‘hello‘,‘index/hello‘);
Please add use think\Route
or use your own
\think\Route::get(‘hello‘,‘index/hello‘);
Automatic registration
We only need to put our class library package directory in the EXTEND_PATH
directory (by default extend
, configurable), You can automatically register the corresponding namespace, for example:
We extend
add a new directory below the directory my
, and then define a \my\Test
class (the class file is located extend/my/Test.php
) as Follows:
namespace my;class Test { public function sayHello() { echo ‘hello‘; }}
We can instantiate and invoke them directly:
$Test = new \my\Test();$Test->sayHello();
If we redefine the constants in the application portal file, we EXTEND_PATH
can also change \my\Test
the location of the class file, for example:
define(‘EXTEND_PATH‘,‘../vendor/‘);
Then \my\Test
the location of the class file becomes /vendor/my/File.php
.
Manual Registration
You can also register a new root namespace by registering it manually, for example:
Add the following code to the application portal File:
\think\Loader::addNamespace(‘my‘,‘../application/extend/my/‘);
If you want to register multiple root namespaces at the same time, you can use:
\think\Loader::addNamespace([ ‘my‘ => ‘../application/extend/my/‘, ‘org‘ => ‘../application/extend/org/‘,]);
You can also add a configuration directly to the App's profile, which is automatically registered when the app Executes.
‘root_namespace‘ => [ ‘my‘ => ‘../application/extend/my/‘, ‘org‘ => ‘../application/extend/org/‘,]
Apply Class Library Package
To avoid and Composer
automatically load the class inventory in conflict, the root of the application class Library's namespace is uniformly app
named, for example:
namespace app\index\model;class User extends \think\Model{}
Its class file is located application/index/model/User.php
.
namespace app\admin\event;class User {}
Its class file is located application/admin/event/User.php
.
If you feel that the app
root namespace is inappropriate or conflicting, you can modify it in the app configuration file:
‘app_namespace‘ => ‘application‘,
Once defined, the namespace of the Application class library is changed to:
namespace application\index\model;class User extends \think\Model{}
namespace aliases
The framework allows aliases to be defined for namespaces, such as:
namespace app\index\model;use think\Model;class User extends Model{}
Originally in the controller inside the call mode is:
namespace app\index\controller;use app\index\model\User;class Index{ public function index() { $user = new User(); }}
If we register the namespace alias in the app public file as Follows:
\think\Loader::addNamespaceAlias(‘model‘,‘app\index\model‘);
then, the above controller code can be changed to:
namespace app\index\controller;use model\User;class Index{ public function index() { $user = new User(); }}
thinkphp 5.0 Namespaces