Home php教程 php手册 ThinkPHP之import方法实例详解

ThinkPHP之import方法实例详解

Jun 13, 2016 am 09:30 AM
thinkphp

import方法是ThinkPHP框架用于类库导入的封装实现,尤其对于项目类库、扩展类库和第三方类库的导入支持,import方法早期的版本可以和java的import方法一样导入目录和通配符导入,后来考虑到性能问题,在后续的版本更新中不断改进和简化了,所以现在的用法比较简单明了。调用格式:

import('类库名', '起始路径', '类库后缀')

imprt方法有一个别名vendor方法,专门用于导入第三方类库,区别在于起始路径和类库后缀默认值不同。

我们来分析下具体的用法:

1.导入系统基类库

系统基类库其实就是指的Think类库包,所在目录就是指框架的核心Lib目录,import方法可以用于导入系统基类库,例如:

import('Think.Util.Array');

Copy after login

表示导入系统目录下面的Lib/Util/Array.class.php 类库文件,相当于我们这样使用

require THINK_PATH.'Lib/Util/Array.class.php';

Copy after login

可以支持多级目录,例如:

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

Copy after login

通过import方法导入类库后,就可以进行类库的实例化操作了。

2.导入扩展类库

扩展类库位于Extend/Library目录下面,这是系统的公共扩展类库目录,目前支持的扩展类库包只有ORG和Com包。

import('ORG.Util.Image');
import('Com.Sina.OAuth');

Copy after login

会导入扩展目录下面的第三方类库(分别是Extend/Library/ORG/Util/Image.class.php和Extend/Library/Com/Sina/OAuth.class.php 类库文件),第三方类库包只能支持ORG和Com两种,下面的子目录可以随意添加。

3.导入项目应用类库

如果没有指定起始导入路径的话,类库包Think、ORG、Com之外的都会被认为是导入项目应用类库,例如:

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

Copy after login

表示导入MyApp项目的UserAction和InfoModel类库文件,由于通常,我们都是导入当前项目下面的类库,所以可以简写成:

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

Copy after login

@符号表示导入当前项目下面的类库,这种方式也一定程度上方便了项目类库的代码移植,如果项目名称改变或者移动到其它项目下面的时候,写法不需要改变。

4.导入非标准类库文件

这里所说的非标准类库文件,主要是指位于特殊位置或者非.class.php后缀的类库文件。像导入基类库、扩展类库和项目类库都是基于框架规范的目录下面,如果我们需要导入项目的Common目录下面的MyClass.php文件,则可以采用:

import('Common.MyClass',APP_PATH,'.php');

Copy after login

或者

import('MyClass',APP_PATH.'Common','.php');

Copy after login

或者要导入当前目录下面的RBAC类库

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

Copy after login

还有一种特殊情况,是类库命名的特殊性。按照系统的规则,import方法是无法导入具有点号的类库文件的,因为点号会直接转化成斜线,例如我们定义了一个名称为User.Info.class.php 的文件的话,采用:

import("ORG.User.Info");

Copy after login

方式加载的话就会出现错误,导致加载的文件不是ORG/User.Info.class.php 文件,而是ORG/User/Info.class.php 文件,这种情况下,我们可以使用:

import("ORG.User#Info");

Copy after login

来导入。

5.第三方类库导入

ThinkPHP 的基类库都是以.class.php 为后缀的,这是系统内置的一个约定,当然也可以通过 import 的参数来控制, 为了更加方便引入其他框架和系统的类库, 系统还提供了一个import方法的别名vendor,专门用于导入第三方类库,并且默认的起始目录和类文件后缀有区别。第三方类库位于系统扩展目录下的Vendor 目录, 例如,我们把 Zend 的 Filter\Dir.php 放到 Vendor 目录下面,这个时候 Dir 文件的路径就是 Vendor\Zend\Filter\Dir.php,我们使用vendor 方法导入只需要使用:

Vendor('Zend.Filter.Dir');

Copy after login

就可以导入Dir类库了。
Vendor方法也可以支持和import方法一样的基础路径和文件名后缀参数,例如:

Vendor('Zend.Filter.Dir',dirname(__FILE__),'.class.php');

Copy after login

6.别名导入

除了命名空间的导入方式外,import方法还可以支持别名导入,要使用别名导入,首先要定义别名,我们可以在项目配置目录下面增加alias.php 用以定义项目中需要用到的类库别名,例如:

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

Copy after login

那么,现在就可以直接使用:

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

Copy after login

导入Rbac和Page类,别名导入方式禁止使用import方法的第二和第三个参数,别名导入方式的效率比命名空间导入方式要高效,缺点是需要预先定义相关别名。
可以为某些需要的类库定义别名,那么无需定义自动加载路径也可以快速的自动加载。

一般情况下,由于框架内部采用了自动加载方式,所以大多数情况下面不需要用户手动导入类库文件,通常用于导入扩展类库和第三方类库的情况居多。而且配合别名定义和自动加载路径的定义,也能减少用户手动导入类库的情况。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

Development suggestions: How to use the ThinkPHP framework for API development Development suggestions: How to use the ThinkPHP framework for API development Nov 22, 2023 pm 05:18 PM

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.

See all articles