ThinkPHP simulates the Java Class Library import mechanism and uniformly uses the import method to load class files. The import method is the built-in class library and file import method of ThinkPHP. It provides a convenient and flexible file import mechanism and can completely replace the require and include methods of PHP. ThinkPHP import method
ThinkPHP simulates the Java Class Library import mechanism and uses the import
Method to load class files. The import method is the built-in class library and file import method of ThinkPHP. It provides a convenient and flexible file import mechanism to replace PHP's require.
And include methods.
However, for third-party class libraries, we recommend that you use the vendor method to import them.
Import
Syntax:
Boolen import (class, baseUrl, ext)
Parameter description: parameter description
Class
Required. it indicates the class library to be imported. it adopts the namespace method.
BaseUrl is optional, indicating the basic path of the import. if it is omitted, the system uses the default rule. See the following for details.
Ext
Optional. it indicates the suffix of the imported class library. the default value is. class. php.
The import method is ThinkPHP.
A built-in common function can be used freely in module operations. for details, refer to PHP include and require usage.
Import a class library
Import
ThinkPHP Base Class Library File
Import the ThinkPHP Base Class Library file using the import method.
The system Base Class Library is the relative starting Directory:
- // Import the ThinkPHP system directory/Lib/Think/Util/Session. class. php file
- Import ("Think. Util. Session ");
- // Import the ThinkPHP system directory/Lib/ORG/Util/Page. class. php file
- Import ("ORG. Util. Page ");
Import the project application library file
- // Import 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 ");
- // Import the MyApp project Lib/ORG/My/Image. class. php file
- Import ("MyApp. ORG. My. Image ");
For the current project, it can be simplified:
- Import ("@. Action. UserAction ");
- Import ("@. ORG. Page ");
- Import ("@. ORG. My. Image ");
This writing method is conducive to the migration of projects. If you are importing a class library of another project, you must specify the project name:
Import ("OtherApp. Action. UserAction ");
Note: When using this method to import the class libraries of other projects, you must ensure that the directories of the two projects are of the same level; otherwise, you must specify
BaseUrl parameters.
Specify baseUrl parameters
For the above import system library and project class library, import
The method automatically identifies the path to import the class library. In other cases, you need to specify the baseUrl parameter. For example, import My. class. php under the Directory where the current file is located.
File:
Import ("My", dirname (_ FILE __));
Ext parameter
Ext
The parameter indicates the suffix of the imported class library. the default value is. class. php. If the imported file is not in *. class. php format, you can specify the ext parameter. For example, import
My. php file:
Import ("My", dirname (_ FILE _), ". php ");
.
Symbol File Import
In the import method, the. symbol indicates the directory level. if the file name to be imported contains A. symbol, such
User. Info. class. php should be replaced.
No:
Import ("@. Action. User # Info ");
Alias import
If the alias is defined in the alias definition file:
'Advmodel'
=> THINK_PATH. '/Lib/Think/Core/Model/AdvModel. class. php ',
You can use import
Method to import the corresponding class library file as an alias:
Import ('advmodel ');
The default alias definition file is located in ThinkPHP.
Commonalias. php in the system directory can also define the project's own alias file.
Other instructions
File import conflict with the same name
Import
The method has a caching and detection mechanism. the same file will not be imported repeatedly. if a file with the same name under the same class library is imported in different locations, the system will prompt a conflict, for example:
Import ("Think. Util. Array ");
Import ("App. Util. Array ");
In the above case, two identical
Array. class. php class, even if the actual class name does not conflict
Naming conventions: the class name and file name are consistent. Therefore, the system will throw an exception that conflicts with the class name and terminate the execution.
When do I need to use the import method?
Import
This method is used to import files that are currently required and cannot be automatically loaded by ThinkPHP, such as the extended base classes in the system base classes ORG and Com directories and their own class libraries. Third-party class libraries can also be used
Import method, but we recommend that you use the vendor method.
Differences between the import method and the import label
Import
The method is used to import file class libraries that cannot be automatically loaded in module operations. the import label is used to import external Js and CSS files in the template.
Import method and PHP6
Import
In future PHP6 versions, namespaces and import keywords are supported. Therefore, ThinkPHP May
Method.