The import method is the encapsulated implementation of the ThinkPHP framework for class library import, especially for the import support of project class libraries, extension class libraries and third-party class libraries. The early version of the import method can import directories and libraries in the same way as the java import method. Wildcard import was subsequently improved and simplified in subsequent version updates due to performance issues, so its current usage is relatively simple and clear. Call format:
import('class library name', 'starting path', 'class library suffix')
The imprt method has an alias vendor method, which is specifically used to import third-party class libraries. The difference is that the starting path and the default value of the class library suffix 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, and the directory it is located in 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');
means importing the Lib/Util/Array.class.php class library file under the system directory, which is equivalent to how we use
require THINK_PATH.'Lib/Util/Array.class.php';
Can support 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 extension class library
The extension class library is located under the Extend/Library directory, which is the public extension class library directory of the system. Currently, the only extension class library packages supported are ORG and Com packages.
import('ORG.Util.Image'); import('Com.Sina.OAuth');
The third-party class library under the extension directory (respectively Extend/Library/ORG/Util/Image.class.php and Extend/Library/Com/Sina/OAuth.class.php class library files) will be imported. Class library packages can only support ORG and Com. The following subdirectories can be added at will.
3. Import project application class library
If the starting import path is not specified, class library packages other than Think, ORG, and Com will be considered as imported project application class libraries, for example:
import("MyApp.Action.UserAction"); import("MyApp.Model.InfoModel");
means importing the UserAction and InfoModel class library files of the MyApp project. Since we usually import the class library under the current project, it can be abbreviated as:
import("@.Action.UserAction"); import("@.Model.InfoModel");
@ symbol means importing the class library under the current project. This method also facilitates the code transplantation of the project class library to a certain extent. If the project name changes or is 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 files mentioned here mainly refer to class library files located in special locations or with non-.class.php suffix. Importing base class libraries, extension class libraries and project class libraries are all based on the directory of the framework specification. If we need to import the MyClass.php file under the Common directory of the project, we can use:
import('Common.MyClass',APP_PATH,'.php');
or
import('MyClass',APP_PATH.'Common','.php');
Or you want to import the RBAC class library in the current directory
import("RBAC.AccessDecisionManager",dirname(__FILE__),".php");
There is another special case, which is the particularity of class library naming. According to the rules of the system, the import method cannot import class library files with dots, because the dots will be directly converted into slashes. For example, if we define a file named User.Info.class.php, use:
import("ORG.User.Info");
method, an error will occur, causing the loaded file not to be the ORG/User.Info.class.php file, but the ORG/User/Info.class.php file. In this case, we can use:
import("ORG.User#Info");
to import.
5. Third-party library import
ThinkPHP’s base class libraries are all suffixed with .class.php. This is a convention built into the system. Of course, it can also be controlled through import parameters. In order to make it easier to introduce class libraries from other frameworks and systems, the system An alias vendor of the import method is also provided, which is specifically used to import third-party class libraries, and the default starting directory and class file suffix are different. The third-party class library is located in the Vendor directory under the system extension directory. For example, we put Zend's FilterDir.php under the Vendor directory. At this time, the path of the Dir file is VendorZendFilterDir.php. When we use the vendor method to import, we only need to use:
Vendor('Zend.Filter.Dir');
You can import the Dir class library.
The Vendor method can also support the same basic 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 namespace import method, the import method can also support alias import. To use alias import, you must first define the alias. We can add alias.php under the project configuration directory to define the classes that need to be used in the project. Library alias, for example:
return array( 'rbac' =>LIB_PATH.'Common/Rbac.class.php', 'page' =>LIB_PATH.'Common/Page.class.php', );
Then, you can use it directly now:
import("rbac"); import("page");
Import Rbac and Page classes. The alias import method prohibits the use of the second and third parameters of the import method. The alias import method is more efficient than the namespace import method. The disadvantage is that the relevant aliases need to be defined in advance.
Aliases can be defined for some required class libraries, so they can be automatically loaded quickly without defining an automatic loading path.
Generally, due to the automatic loading method used within the framework, users do not need to manually import class library files in most cases. It is usually used to import extension class libraries and third-party class libraries. Moreover, with the definition of alias definition and automatic loading path, it can also reduce the need for users to manually import class libraries.