thinkphp namespace Usage example, thinkphp namespace _php tutorial

Source: Internet
Author: User

thinkphp namespace usage examples, thinkphp namespaces


This example describes the Thinkphp namespace usage. Share to everyone for your reference, as follows:

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:
Copy the Code code 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{public   function 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 Usermodel 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:
Copy the Code Code as follows: ' 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 Usermodel 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);

It is hoped that this article is helpful to the PHP program design based on thinkphp framework.

Articles you may be interested in:

    • thinkphp AutoLoad Namespace Customization namespace
    • Namespaces in PHP are described in detail
    • A brief analysis of namespace-related concepts in PHP
    • Dynamic access and usage tips for PHP namespaces (namespace)
    • PHP Namespaces (namespace) Usage basics and examples
    • A concise tutorial on PHP namespaces (Namespace)
    • PHP Namespace Learning Detailed
    • The use of PHP namespaces (Namespace) is detailed
    • PHP 5.3 New Feature namespace rule parsing and advanced features

http://www.bkjia.com/PHPjc/1086638.html www.bkjia.com true http://www.bkjia.com/PHPjc/1086638.html techarticle thinkphp Namespace usage examples, thinkphp namespaces This article describes the thinkphp namespace usage. Share to everyone for your reference, as follows: New version (3.2) ...

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