Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use import in thinkphp

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to use import in thinkphp", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use import in thinkphp" article.

In thinkphp, the import method is used for the encapsulation implementation of class library import, which can provide import support for project class libraries, extended class libraries and third-party class libraries, with the syntax of "import ('class library name', 'starting path', 'class library suffix')"; this method has individual names of vendor methods, which are specially used to import third-party class libraries.

This article operating environment: Windows10 system, ThinkPHP3.2 version, Dell G3 computer.

What is the use of import in thinkphp

Import method is the encapsulation implementation of ThinkPHP framework for class library import, especially for the import support of project class library, extended class library and third-party class library. The early version of import method can import directories and wildcards like java's import method. Later, considering performance problems, it has been continuously improved and simplified in subsequent version updates, so the current usage is relatively simple and clear. Call format:

Import ('class library name', 'start path', 'class library suffix')

The imprt method has an alias vendor method that is specifically used to import third-party class libraries, except that the starting path and the class library suffix default values are different.

Let's analyze the specific usage:

1. Import system base class library

The system base class library actually refers to the Think class library package, and the directory 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')

Indicates that the Lib/Util/Array.class.php class library file under the system directory is imported, which is equivalent to the way we use

Require THINK_PATH.'Lib/Util/Array.class.php'

Multi-level directories can be supported, such as:

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.

two。 Import extended class library

The extension class library is located under the Extend/Library directory, which is the system's common extension class library directory. 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 libraries under the extension directory (Extend/Library/ORG/Util/Image.class.php and Extend/Library/Com/Sina/OAuth.class.php class library files, respectively) will be imported. The third-party class library package can only support ORG and Com, and the following subdirectories can be added at will.

3. Import project application class library

If the initial import path is not specified, everything other than the class library package Think, ORG, and Com will be considered to import the project application class library, for example:

Import ("MyApp.Action.UserAction"); import ("MyApp.Model.InfoModel")

Represents the UserAction and InfoModel class library files imported into the MyApp project. Since we usually import the class libraries under the current project, we can simply write them as follows:

Import ("@ .Action.UserAction"); import ("@ .Model.InfoModel")

The @ symbol means to import the class library under the current project, which also facilitates the code migration of the project class library to some extent. If the project name is changed or 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 the class library files located in a special location or with a non-.class.php suffix. For example, importing base class libraries, extension class libraries, and project class libraries are all under the directory based on 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 to import the RBAC class library under the current directory

Import ("RBAC.AccessDecisionManager", dirname (_ _ FILE__), ".php")

There is also a special case of the particularity of class library naming. According to the rules of the system, the import method cannot import a class library file with a period, because the period is directly converted to a slash. For example, if we define a file named User.Info.class.php, use:

Import ("ORG.User.Info")

Method, there will be an error, resulting in the loaded file is not an ORG/User.Info.class.php file, but an ORG/User/Info.class.php file, in which case, we can use:

Import ("ORG.User#Info")

To import.

5. Third-party class library import

The base class libraries of ThinkPHP are suffixed with. Class.php, which is a built-in convention of the system, of course, it can also be controlled by the parameters of import. In order to facilitate the introduction of other frameworks and system class libraries, the system also provides an alias vendor of the import method, which is specially 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, if we put the Filter\ Dir.php of Zend under the Vendor directory, the path to the Dir file is Vendor\ Zend\ Filter\ Dir.php. We only need to use the vendor method to import:

Vendor ('Zend.Filter.Dir')

You can import the Dir class library.

The Vendor method can also support the same underlying 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 namespace import, the import method can also support alias import. To use alias import, we first need to define aliases. We can add alias.php under the project configuration directory to define the class library aliases needed in the project, for example:

Return array ('rbac' = > LIB_PATH.'Common/Rbac.class.php',' page' = > LIB_PATH.'Common/Page.class.php',)

So, you can use it directly now:

Import ("rbac"); import ("page")

When importing Rbac and Page classes, alias import forbids the use of the second and third parameters of the import method. Alias import is more efficient than namespace import, but the disadvantage is that relevant aliases need to be defined in advance.

You can define aliases for some required class libraries, so you can load quickly and automatically without defining an autoload path.

In general, because the automatic loading mode is used within the framework, in most cases, users do not need to manually import class library files, which are usually used to import extended class libraries and third-party class libraries. And combined with alias definition and auto-load path definition, it can also reduce the situation that users manually import class libraries.

The above is about the content of this article on "how to use import in thinkphp". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report