ThinkPHP import method example _ php instance

Source: Internet
Author: User
Tags oauth
The import method is the encapsulation implementation of ThinkPHP framework used for class library import. This article mainly introduces the import method of ThinkPHP. if you need it, refer to the import method below. the ThinkPHP framework is used for the encapsulation implementation of class library import, especially for the import support of Project class libraries, extended class libraries, and third-party class libraries, earlier versions of the import method can import directories and wildcards like the java import method. later, considering the performance problems, it is constantly improved and simplified in subsequent version updates, so the current usage is relatively simple and clear. Call format:

Import ('class Library name', 'Start path ', 'class Library suffixes ')

The imprt method has an alias for the vendor method, which is used to import third-party class libraries. The difference is that the default value of the starting path and class library suffix is different.

Let's analyze the specific usage:

1. import the system Base Class Library

The system Base Class Library actually refers to the Think class library package, and the directory refers to the core Lib Directory of the framework. the import method can be used to import the system Base Class Library, for example:

import('Think.Util.Array');

Import the Lib/Util/Array. class. php class library file under the system directory, which is equivalent

require THINK_PATH.'Lib/Util/Array.class.php';

Supports multi-level directories, for example:

import('Think.Util.U1.ClassA');import('Think.Util.U1.A2.ClassB');

After importing the class library through the import method, you can instantiate the class library.

2. import the extension class library

The extension class Library is located under the Extend/Library Directory, which is the system's public Extension class Library directory. Currently, only ORG and Com packages are supported.

import('ORG.Util.Image');import('Com.Sina.OAuth');

The third-party class libraries (Extend/Library/ORG/Util/Image) under the extension directory will be imported. class. php and Extend/Library/Com/Sina/OAuth. class. php class library files), third-party class library packages can only support ORG and Com, the following sub-directories can be added at will.

3. import the project application library

If the start import path is not specified, all the class library packages other than Think, ORG, and Com are considered as the import project application class libraries. for example:

import("MyApp.Action.UserAction");import("MyApp.Model.InfoModel");

It indicates the UserAction and InfoModel class library files of the MyApp project to be imported. because we usually import the class libraries under the current project, it can be abbreviated:

import("@.Action.UserAction");import("@.Model.InfoModel");

@ Symbol indicates to import the class libraries under the current project. this method also facilitates code migration of the project class library to a certain extent. if the project name is changed or moved to another project, the writing method does not need to be changed.

4. import non-standard class library files

The non-standard class library file mentioned here mainly refers to the class library file located in a special location or not suffixed with. class. php. For example, the imported base class libraries, extended class libraries, and project class libraries are all in the directory based on the framework specification. if you need to import the MyClass. php file under the Common Directory of the project, you can use:

import('Common.MyClass',APP_PATH,'.php');

Or

import('MyClass',APP_PATH.'Common','.php');

Or import the RBAC class library under the current directory.

import("RBAC.AccessDecisionManager",dirname(__FILE__),".php");

Another special case is the particularity of class library naming. According to the system rules, the import method cannot import a class library file with a dot number, because the DOT number is directly converted into a diagonal line. for example, we have defined a name as User. info. class. php files:

import("ORG.User.Info");

The file to be loaded is not ORG/User. info. class. php file, but ORG/User/Info. class. php file. in this case, we can use:

import("ORG.User#Info");

.

5. third-party class library import

The base class libraries of ThinkPHP are. class. php is a suffix, which is a built-in convention of the system. of course, it can also be controlled by the import parameter. to facilitate the introduction of other frameworks and system class libraries, the system also provides an alias for the import method, which is used to import third-party class libraries. the default start directory differs from the class file suffix. The third-party class library is located in the Vendor directory under the system extension directory. for example, we use the Zend Filter \ Dir. put php under the Vendor Directory. at this time, the path of the Dir file is Vendor \ Zend \ Filter \ Dir. php. we only need to use the following to import data using the vendor method:

Vendor('Zend.Filter.Dir');

You can import the Dir class library.
The Vendor method also supports the same basic path and filename suffix parameters as the import method, for example:

Vendor('Zend.Filter.Dir',dirname(__FILE__),'.class.php');

6. alias import

In addition to the namespace import method, the import method also supports alias import. to use alias import, you must first define the alias. you can add alias under the project configuration directory. php defines the class library alias to be used in the project, for example:

return array(  'rbac' =>LIB_PATH.'Common/Rbac.class.php',  'page' =>LIB_PATH.'Common/Page.class.php', );

Now you can directly use:

import("rbac");import("page");

The second and third parameters of the import method are not allowed in the alias import method. the alias import method is more efficient than the namespace import method, the disadvantage is that related aliases need to be pre-defined.
You can define aliases for some required class libraries, so you can quickly automatically load without defining an automatic loading path.

Generally, the framework uses the automatic loading method. In most cases, you do not need to manually import the class library file. Generally, this method is used to import extension class libraries and third-party class libraries. In addition, the alias definition and the automatic loading path definition can also reduce the manual import of class libraries.

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.