Examples of thinkphp namespace usage, thinkphp namespace _ PHP Tutorial

Source: Internet
Author: User
Thinkphp namespace usage example, thinkphp namespace. The thinkphp namespace usage example describes thinkphp namespace usage in this article. The thinkphp namespace usage examples in the new version (3.2) are described as follows:

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.

Articles you may be interested in:
  • Thinkphp autoload namespace custom namespace
  • PHP namespace details
  • Introduction to namespaces in PHP
  • PHP namespace dynamic access and usage skills
  • Usage basics and examples of PHP namespaces
  • Simple PHP Namespace tutorial
  • Php namespace learning details
  • PHP Namespace usage
  • New namespace rule parsing and advanced functions in PHP 5.3

Examples in this article describe the usage of thinkphp namespace. Share it with you for your reference. The details are as follows: New version (3.2...

Related Article

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.