Namespace code of thinkPHP5.0 framework

Source: Internet
Author: User
Tags traits
This article mainly introduces the thinkPHP5.0 framework namespace, and analyzes in detail the concepts, functions, and usage of the namespace in thinkPHP5.0 based on the specific instance form, for more information about the namespace of thinkPHP5.0 framework, see the next article. the concepts, functions, and usage of the namespace in thinkPHP5.0 are analyzed in detail based on the specific instance form, for more information, see

This article describes the thinkPHP5.0 framework namespace. We will share this with you for your reference. The details are as follows:

Namespace

ThinkPHP defines and automatically loads class library files using namespaces, effectively solving namespace conflicts between multiple modules and Composer class libraries, and implementing a more efficient automatic loading mechanism for class libraries.

If you do not know the basic concepts of the namespace, refer to the PHP Manual: PHP namespace

Note that if you need to call a PHP built-in class library or a third party does not use a namespace class library, remember to add \ when instantiating the class library, for example:

// Incorrect usage $ class = new stdClass (); $ xml = new SimpleXmlElement ($ xmlstr); // correct usage $ class = new \ stdClass (); $ xml = new \ SimpleXmlElement ($ xmlstr );

In ThinkPHP5.0, you only need to correctly define the namespace for the class library, and the namespace path is the same as the Directory of the class library file, then the class can be automatically loaded, so as to realize real inert loading.

For example, the definition of the \ think \ cache \ driver \ File class is:

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 in the corresponding path of the class. The path is thinkphp/library/think/cache/driver/File. php.

5.0 the default directory specification is lower-case. class files are named in the camper mode and the first letter is in the upper case.

In principle, you can support directories named by the hump method, as long as the namespace definition is consistent with the directory, for example:

We instantiate

$class = new \Think\Cache\Driver\File();

The system automatically loads the thinkphp/library/Think/Cache/Driver/File. php File.

Root namespace (class library package)

Root namespace is a key concept. the above \ think \ cache \ driver \ File class is used 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 that a root namespace corresponds to a class library package.

Several built-in namespaces (class library packages) 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 Library Application


To add a new root namespace, you can use either of the following methods:Register a new root namespaceOrPut in the EXTEND_PATH Directory (automatically register).

Note that the sample code in this article is concise. if the namespace of the class library is not specified, it indicates the think namespace.For example, the following code:

Route::get('hello','index/hello');

Please use it by yourself:

use think\Route

Or:

\think\Route::get('hello','index/hello');

Automatic registration

We only need to put the directory of our class library package into the EXTEND_PATH Directory (extend, configurable) to automatically register the corresponding namespace, for example:

We add a my directory under the extend directory and define a \ my \ Test class (the class file is located in extend/my/Test. php) as follows:

namespace my;class Test{ public function sayHello() {  echo 'hello'; }}

We can directly instantiate and call:

$Test = new \my\Test();$Test->sayHello();

If we re-define the EXTEND_PATH constant in the application entry file, we can also change the position of the \ my \ Test class file, for example:

define('EXTEND_PATH','../vendor/');

The path of the \ my \ Test class File is/vendor/my/File. php.

Manual registration

You can also manually register a new root namespace, for example:

Add the following code to the application entry file:

\think\Loader::addNamespace('my','../application/extend/my/');

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 the configuration directly to the application configuration file. The system automatically registers the configuration when the application is executed.

'root_namespace' => [ 'my' => '../application/extend/my/', 'org' => '../application/extend/org/',]

Application Library package

To avoid conflicts with the class library automatically loaded by Composer, the root of the namespace of the application class library is named by the app.For example:

namespace app\index\model;class User extends \think\Model{}

The class file is located in application/index/model/User. php.

namespace app\admin\Event;class User{}

The class file is located in application/admin/event/User. php.

If the root namespace of the app is inappropriate or conflicted, you can modify it in the application configuration file:

'app_namespace' => 'application',

After definition, the namespace of the application class library is changed:

namespace application\index\model;class User extends \think\Model{}

Namespace alias

The framework allows you to define aliases for namespaces, for example:

namespace app\index\model;use think\Model;class User extends Model{}

The original calling method in the controller 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 application public file:

\think\Loader::addNamespaceAlias('model','app\index\model');

Then, the controller code above can be changed:

namespace app\index\controller;use model\User;class Index{ public function index() {  $user = new User(); }}

The above is a detailed description of the namespace code of thinkPHP5.0 framework. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.