In thinkphp, the import method is used to implement the encapsulation of class library import. It can provide import support for project class libraries, extended class libraries and third-party class libraries. The syntax is "import('class library name', 'Start path', 'Class library suffix')"; This method has an alias vendor method, which is specially used to import third-party class libraries.
The operating environment of this article: Windows 10 system, ThinkPHP version 3.2, Dell G3 computer.
The import method is the encapsulation 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 wildcard imports like the Java import method. Later, considering performance issues, it was continuously improved and simplified in subsequent version updates, so the current usage is relatively simple and clear. Calling format:
import('类库名', '起始路径', '类库后缀')
The imprt method has an alias vendor method, which is specially 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 The directory where the package is located 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 us using
require THINK_PATH.'Lib/Util/Array.class.php';
to 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, the class library can be instantiated.
2. Import the extension class library
The extension class library is located under the Extend/Library directory. This is the public extension class library directory of the system. Currently supported extension class libraries The packages are only ORG and Com packages.
import('ORG.Util.Image'); import('Com.Sina.OAuth');
will import 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 ), third-party 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 imported. Project application class library, 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");
4. Import non-standard class library files
The non-standard class library files mentioned here mainly refer to those located in special locations or with non-.class.php suffixes Class library file. 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');
import('MyClass',APP_PATH.'Common','.php');
import("RBAC.AccessDecisionManager",dirname(__FILE__),".php");
import("ORG.User.Info");
way, 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 class library importThinkPHP’s base class libraries are all suffixed with .class.php. This is a built-in convention of the system. Of course It can also be controlled through the import parameters. In order to make it easier to introduce class libraries from other frameworks and systems, the system also provides an alias vendor for the import method, which is specially used to import third-party class libraries, and the default starting directory and class file The suffixes are different. The third-party class library is located in the Vendor directory under the system extension directory. For example, we put Zend's Filter\Dir.php under the Vendor directory. At this time, the path of the Dir file is Vendor\Zend\Filter\Dir.php. We use vendor For method import, you only need to use:
Vendor('Zend.Filter.Dir');
to 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');
Except 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 class library alias that needs to be used in the project, 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 efficiency of the alias import method is It is more efficient than the namespace import method, but the disadvantage is that related aliases need to be defined in advance.
You can define aliases for some required class libraries, so they can be automatically loaded quickly without defining an automatic loading path.
Generally, since the automatic loading method is adopted inside the framework, in most cases users do not need to manually import class library files, which are 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.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the usage of import in thinkphp. For more information, please follow other related articles on the PHP Chinese website!