Thinkphp Import Method Example _php tutorial

Source: Internet
Author: User
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 just like the Java import method, which later takes into account performance issues. It has been improved and simplified in subsequent versions of the update, so the usage is simple and straightforward. 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 third-party class libraries, except that the starting path and the class library suffix default values are 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, where the directory refers to the framework's core LIB directory, the import method can be used to import the system base Class library, 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 using

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

Multi-level catalogs can be supported, for example:

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

After importing the class library through the import method, you are ready to instantiate the class library.

2. Import the Extension class library

The Extension class library is located under the Extend/library directory, which is the public extension class library directory for the system 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 library (extend/library/org/util/image.class.php and extend/library/com/sina/oauth.class.php class library files) under the extended directory, respectively, Third-party class library packages can only support org and COM two, and the following subdirectories may be added at will.

3. Import the Project Application class library

If you do not specify a starting import path, the class library package think, ORG, COM will be considered to be imported into the Project Application class library, for example:

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

Represents the Useraction and Infomodel class library files that import the MyApp project, since we are usually importing the class library below the current project, so we can write:

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

The @ symbol means that importing the class library below the current project is also somewhat convenient for code porting of the Project class library, which does not need to be changed if the project name changes or moves under other projects.

4. Import non-standard class library files

The nonstandard class library file referred to here mainly refers to a class library file located in a special location or a non-. class.php suffix. Like the import base Class library, the Extension class library, and the Project class library are all based on the framework specification, if we 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, to import the RBAC class library under the current directory

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

There is also a special case, which 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 converted directly to a slash, for example, we define a file named User.Info.class.php, using:

Import ("ORG. User.info ");

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

Import ("ORG. User#info ");

To import.

5. Third-party class library import

Thinkphp's base Class library is a. class.php suffix, which is a built-in convention, of course, can also be controlled by the import parameters, in order to facilitate the introduction of 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 suffix are different. Third-party class libraries are 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 base path and file name 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 under the project configuration directory to define the class library aliases 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 use it directly:

Import ("RBAC"); Import ("page");

Import RBAC and page classes, alias import prevents the use of the second and third parameters of the import method, the efficiency of the alias import is more efficient than the namespace import, and the disadvantage is that the related aliases need to be predefined.
You can define aliases for some of the required class libraries, so you can quickly load automatically without defining an auto-load path.

In general, since automatic loading is used within the framework, most cases do not require the user to manually import the class library files, typically for importing extended class libraries and third-party class libraries. Also, the combination of alias definition and automatic load path definition can reduce the user's manual import of the class library.

http://www.bkjia.com/PHPjc/825503.html www.bkjia.com true http://www.bkjia.com/PHPjc/825503.html techarticle 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 the earlier version of the import method can be used with Java's ...

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