Example of thinkphp Import method _php Example

Source: Internet
Author: User
Tags aliases extend oauth php class versions zend

The import method is an encapsulation implementation of the thinkphp framework for Class library imports, especially for import support for Project class libraries, extended class libraries, and Third-party class libraries, and earlier versions of the Import method can import directories and wildcard imports like Java import methods, and then consider performance issues. Improved and simplified in subsequent versions, so the usage is simpler and more straightforward now. Call Format:

Import (' Class library name ', ' Start path ', ' Class library suffix ')

The Imprt method has an alias vendor method that is specifically used to import a Third-party class library, except that the start path and class library suffix defaults are different.

Let's analyze the specific usage:

1. Import System base Class Library

The system base Class library actually refers to the Think Class library package, where the directory refers to the core Lib directory of the framework, and the import method can be used to import system base class libraries, for example:

Import (' Think.Util.Array ');

Represents the Lib/util/array.class.php class library file under the Import system directory, which is equivalent to the way we use

Require Think_path. ' lib/util/array.class.php ';

Multilevel catalogs can be supported, for example:

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

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

2. Import Extended Class Library

The Extended class library is located under the Extend/library directory, which is the system's public extended class library directory, and currently supports only Org and COM packages for the Extended class library package.

Import (' ORG. Util.image ');
Import (' Com.Sina.OAuth ');

Will import the third party class libraries (extend/library/org/util/image.class.php and extend/library/com/sina/oauth.class.php class library files, respectively) below the extended directory. The third party Class library package can only support org and COM two, and the following subdirectories are optionally added.

3. Import Project Application Class Library

If you do not specify a start import path, the class library package think, ORG, COM is considered an import Project Application class library, for example:

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

Represents the Useraction and Infomodel class library files that import MyApp projects, since we are typically importing the class libraries below the current project, so we can write a simple:

Import ("@. Action.useraction ");
Import ("@.model.infomodel");

The @ symbol indicates that the class library under the current project is imported, which in turn facilitates code porting of the Project class library, and does not need to be changed if the project name changes or moves below the other item.

4. Import non-standard class library files

The Non-standard class library file mentioned here refers to a class library file that is located in a special location or a non. class.php suffix. Like the import base Class library, the Extended class library, and the Project class library are all based on the framework specification directory, and if we need to import the myclass.php file below the common directory of the project, you can use the following:

Import (' Common.myclass ', App_path, '. php ');

Or

Import (' MyClass ', App_path. ' Common ', '. php ');

Or you want to import the RBAC class library under the current directory

Import ("RBAC.") Accessdecisionmanager ", DirName (__file__),". php ");

There is also a special case, is the particularity of class library naming. According to the rules of the system, the import method is unable to import the class library file with the dot number, because the dot will be directly converted into a slash, for example, we define a file called User.Info.class.php, using:

Import ("ORG.") User.info ");

The way load will be an error, resulting in the loaded file is not a org/user.info.class.php file, but a org/user/info.class.php file, in which case we can use:

Import ("ORG.") User#info ");

To import.

5. Third-party class library import

The base Class library of Thinkphp is a. class.php suffix, this is a built-in contract, of course, can also be controlled by import parameters, in order to more easily introduce other frameworks and systems of the class library, the system also provides an import method alias vendor, Specifically for importing Third-party class libraries, and the default starting directory and class file suffixes are different. The third party class library is located in the vendor directory under the system extension directory, for example, we put Zend filter\dir.php under the vendor directory, this time the Dir file path is vendor\zend\filter\dir.php, we use ven Dor method Import only needs to use:

Vendor (' Zend.Filter.Dir ');

You can import the Dir class library.
The vendor method can also support the same underlying 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 way namespaces are imported, the import method can also support alias import, to use alias import, first to define aliases, we can add alias.php to define the class library aliases to be used in the project under the project configuration directory, for example:

Return Array (
  ' Rbac ' =>lib_path ' common/rbac.class.php ',
  ' page ' =>lib_path. ' common/page.class.php ',
 );

Now, you can use it directly:

Import ("Rbac");
Import ("page");

Import RBAC and page classes, alias Import methods prohibit the use of the import method of the second and third parameters, alias import method is more efficient than the method of namespace import, the disadvantage is the need to predefined related aliases.
You can define aliases for some of the required class libraries, so you can quickly and automatically load them without having to define an automatic load path.

In general, because of the automatic loading method inside the framework, most cases do not require users to manually import class library files, typically used to import extended class libraries and Third-party class libraries. The definition of alias and automatic loading path can also reduce the user's manual import of the class library.

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.