Thinkphp Learning Note 8-namespaces

Source: Internet
Author: User

Original: thinkphp Learning note 8-namespaces

The new version (3.2) uses namespaces to define and load class library files, solve conflicts between multiple modules, and implement a more efficient automatic loading mechanism.

You need to define the namespace where the class library is defined, and the path to the namespace is consistent with the directory of the class library file, so you can implement automatic loading of the class, for example, the Org\util\file class is defined as

namespace Org\util; class File  {}

The path to which it is located is thinkphp/library/org/util/file.class.php, and we instantiate the class as follows:

$class new \org\util\File();

The above file is loaded automatically so that you do not need to import the class library file before instantiating the class defined by the namespace.

The root namespace is a key concept, with the Org\util\file class above as an example, the org is a root namespace, and its corresponding initial namespace directory is the system's class library directory thinkphp/liberary, the next level subdirectory is automatically recognized as the root namespace, These namespaces can be used without registering.

We add a new my root namespace directory under the Library directory, and then define a test class as follows:

namespace My; class Test {        publicfunction  SayHello ()    {                echo ' Hello ';        }}    

Save the Test class in thinkphp/liberary/my/test.class.php, and we can instantiate and invoke it directly.

$Test New \my\test (); $Test->sayhello ();

The class library namespace in the module is named with the module name, for example:

namespace Home\model; class extends \think\model {}

Its class files are located in application/home/model/usermodel.class.php

namespace Admin\event; class userevent {}

Its class files are located in application/admin/event/userevent.class.php

3.2.1 Version above allows setting to the App class library without using namespaces, set the following in the configuration file:

' app_use_namespace '    =    false,

It is no longer necessary to use a namespace definition in the Application class library, but it is still necessary to use a namespace when inheriting and invoking the core class library, for example, the following Application class library will no longer write namespace Admin\model;

class extends \think\model {}

Special NOTE: If you need to instantiate a PHP built-in class library in version 3.2 or a third-party class that does not use a namespace definition, you need to use the following method:

$class =    new  \stdclass (); $sxml  =    new \simplexmlelement ($xmlstr);

Thinkphp Learning Note 8-namespaces

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: 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.