Thinkphp namespace usage example

Source: Internet
Author: User
: This article describes how to use thinkphp namespaces. For more information about PHP tutorials, see. This example describes the usage of thinkphp namespace. We will share this with you for your reference. The details are as follows:

In the new version (3.2), class library files are defined and loaded using namespaces to resolve conflicts between multiple modules and implement a more efficient automatic loading mechanism.

You need to define the namespace for the class library. if the namespace path is the same as the Directory of the class library File, you can automatically load the class. for example, the Org \ Util \ File class is defined

namespace Org\Util;class File {}

The path is ThinkPHP/Library/Org/Util/File. class. php. The instantiation method is as follows:

The code is as follows:

$ Class = new \ Org \ Util \ File ();


The system automatically loads the above files, so you do not need to import the class library file before instantiating the namespace-defined class.

The root namespace is a key concept. for the above Org \ Util \ File Class, Org is a root namespace, the corresponding initial namespace directory is the system's class library directory ThinkPHP/Liberary. the sub-directories under this directory are automatically recognized as the root namespace, which can be used without registration.

We add a My root namespace directory under the Library Directory and define a Test class as follows:

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

Save the test class in ThinkPHP/Liberary/My/Test. class. php, and we can directly instantiate and call

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

The class library namespace in the module is named by the module name. for example:

namespace Home\Model;class UserModel extends \Think\Model{}

The class file is located in Application/Home/Model/UserModel. class. php.

namespace Admin\Event;class UserEvent {}

The class file is located in Application/Admin/Event/UserEvent. class. php

3.2.1 or later versions allow you to set no namespace for the application library, as shown in the configuration file:

The code is as follows:

'App _ USE_NAMESPACE '=> false,


In this way, the namespace definition is no longer needed in the application class library, but the namespace still needs to be used to inherit and call the core class library. for example, the following application class library will not write the namespace Admin \ Model;

class UserModel extends \Think\Model {}

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

$class =  new \stdClass();$sxml =  new \SimpleXmlElement($xmlstr);

I hope this article will help you design PHP programs based on the thinkPHP framework.

The above introduces the thinkphp namespace usage examples, including the content, hope to be helpful to friends who are interested in PHP tutorials.

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.