thinkPHP5.0 Framework Namespace Code

Source: Internet
Author: User
Tags traits
This article mainly introduces the thinkPHP5.0 framework namespace, combined with concrete instance form to analyze the concept, function and related use method of thinkPHP5.0 in detail, the need of friends can refer to the following

This example describes the thinkPHP5.0 framework namespace. Share to everyone for your reference, as follows:

Name space

Thinkphp uses namespaces to define and automatically load class library files, effectively solves the problem of namespace collisions between multi-module and composer class libraries, and realizes a more efficient class library automatic loading mechanism.

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 namespaces, 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 \SIMPLEXM Lelement ($XMLSTR);

In ThinkPHP5.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, the \think\cache\driver\file 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, with the \think\cache\driver\file class above as an example, think is a root namespace, and its 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 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 to put in the Extend_path directory (autoenrollment).

Note The sample code in this article for brevity, such as the absence of a namespace for the specified class library, means the think namespace , such as the following code:

Route::get (' Hello ', ' Index/hello ');

Please use it yourself:

Use Think\route

Or:

\think\route::get (' Hello ', ' Index/hello ');

Automatic registration

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

We add a new my directory below the Extend directory and then 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 instantiate and invoke them directly:

$Test = new \my\test (); $Test->sayhello ();

If we redefine the Extend_path constant in the application portal file, you can also change the location of the \my\test class file, for example:

Define (' Extend_path ', ' ... /vendor/');

Then the location of the \my\test 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 ' and ' = '. /application/extend/my/', ' org ' = '. /application/extend/org/',]

Apply Class Library Package

to avoid conflicts with composer automatically loaded class inventory, the root of the Application class Library's namespace is uniformly named by the app, for example:

namespace App\index\model;class User extends \think\model{}

Its class files are located in application/index/model/user.php.

Namespace App\admin\event;class user{}

Its class files are located in 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 ();}}

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.