Thinkphp simulates the Java class library import mechanism and uses the import method to load the class file. The import method is thinkphp built-in class library and file import method, provides convenient and flexible file import mechanism, can completely replace PHP's require and include method.
For third-party class libraries, however, we recommend using the vendor method to import.
Import Syntax:
Parameter description:
Parameters |
Description |
Class |
Must represent the class library to be imported, in the way that namespaces are used. |
BaseUrl |
Optional, indicates the base path of the import, omit the system to use the default rules, see below. |
Ext |
Optional, which represents the imported class library suffix, which is. class.php by default. |
The import method is a common function built into the thinkphp, which can be used freely within the module operation, and can be referenced in PHP include and require usage.
Import class Library Importing example import thinkphp base Class library file
The Import method imports the Thinkphp base Class library file, and the contract imports Think, ORG, and Com packages to thinkphp the system base Class library as the relative starting directory:
Import the thinkphp system catalog/lib/think/util/session.class.php file Import ("Think.Util.Session");//import thinkphp system directory/lib/org/util/ Page.class.php file Import ("ORG. Util.page ");
Import Project Application class library file
Imports the MyApp project lib/action/useraction.class.php file import ("MyApp.Action.UserAction");//Import the MyApp project lib/org/ Page.class.php file Import ("MyApp.ORG.Page");//Importing the MYAPP Project lib/org/my/image.class.php file import ("MyApp.ORG.My.Image");
If this is the current project, it can be simplified to:
Import ("@. Action.useraction "), Import (" @.org.page "), Import (" @.org.my.image ");
This writing facilitates the porting of the project. If you are importing a class library for another project, you must specify the project name:
Import ("OtherApp.Action.UserAction");
Note: When you import a class library of another project in this way, you must ensure that the directory for the two items is peer, otherwise you need to specify the BASEURL parameter.
Specifying the BASEURL parameter
For the above imported system class library and project class library, the import method will automatically recognize the path of the class library import, and other cases need to specify the BASEURL parameter. such as importing the My.class.php file in the directory where the current file is located:
Import ("My", DirName (__file__));
Specify EXT Parameters
The ext parameter represents the imported class library suffix, which is. class.php by default. If the imported file is not in *.class.php format, you can specify the EXT parameter. such as importing the my.php file in the directory where the current file is located:
Import ("My", DirName (__file__), ". php");
. Symbol File Import
In the import method, the. The symbol is used to represent the directory hierarchy if the file name that you want to import contains a. symbols, such as User.Info.class.php, need to be replaced with a # number. No.
Import ("@. Action.user#info ");
Alias Import
If an alias is defined in the alias definition file:
' Advmodel ' = Think_path. ' /lib/think/core/model/advmodel.class.php ',
You can import the corresponding class library file by using the import method as an alias:
Import (' Advmodel ');
The default alias definition file for the system is located in the commonalias.php of the thinkphp system directory, and you can define the project's own alias file.
Other description file import conflicts with the same name
The import method has a caching and detection mechanism, and the same file is not repeated for import, and if you find that you have imported a class library file with the same name under a different location, the system will prompt for conflicts such as:
Import ("Think.Util.Array"); Import ("App.Util.Array");
The above situation imports will introduce two Array.class.php classes with the same name, even though the actual class name may not conflict, but according to the thinkphp naming specification, the class name and file name are consistent, so the system throws the exception of class name conflict and terminates execution.
When do I need to use the import method?
The import method is used for importing files that are currently required and thinkphp and cannot be loaded automatically, such as the extended base class in the system base class ORG and Com directories, its own class library, and so on. For third-party class libraries, you can also import using the Import method, but it is recommended that you import using the vendor method.
The difference between the import method and the import label
The import method is used in the module operation for importing file class libraries that cannot be loaded automatically, and the Import tab is used for importing external Js and CSS files in the template.
Import method and PHP6 Import
The namespace and import keywords are supported in the future version of PHP6, so it is foreseeable that thinkphp may overwrite the import method.
Thinkphp Import class library imports include PHP files