Home > PHP Framework > ThinkPHP > body text

What is the usage of import in thinkphp

WBOY
Release: 2022-05-17 11:05:15
Original
2710 people have browsed it

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.

What is the usage of import in thinkphp

The operating environment of this article: Windows 10 system, ThinkPHP version 3.2, Dell G3 computer.

What is the usage of import in thinkphp

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('类库名', '起始路径', '类库后缀')
Copy after login

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');
Copy after login

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';
Copy after login

to support multi-level directories, for example:

import('Think.Util.U1.ClassA');
import('Think.Util.U1.A2.ClassB');
Copy after login

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');
Copy after login

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");
Copy after login

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");
Copy after login
# The ##@ 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 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');
Copy after login

or

import('MyClass',APP_PATH.'Common','.php');
Copy after login

Or you want to import the RBAC class library under the current directory

import("RBAC.AccessDecisionManager",dirname(__FILE__),".php");
Copy after login

There is another special case, which is the particularity of the 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: # If loaded in the ##
import("ORG.User.Info");
Copy after login

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");
Copy after login

to import.

5. Third-party class library import

ThinkPHP’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');
Copy after login

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');
Copy after login

6. Alias ​​import

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',
 );
Copy after login

Then, you can use it directly now:

import("rbac");
import("page");
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!