Home php教程 php手册 Zend Framework教程之Autoloading用法详解

Zend Framework教程之Autoloading用法详解

Jun 06, 2016 pm 07:34 PM
framework zend Tutorial

本文实例讲述了Zend Framework教程之Autoloading用法。分享给大家供大家参考,具体如下: 一、概述 自动加载是一种机制,无需依赖手动编写PHP代码。参考PHP手册自动加载,一旦自动加载器被定义,你试图使用一个没有定义的类或接口的情况下,它会自动被调用。

本文实例讲述了Zend Framework教程之Autoloading用法。分享给大家供大家参考,具体如下:

一、概述

自动加载是一种机制,无需依赖手动编写PHP代码。参考»PHP手册自动加载,一旦自动加载器被定义,你试图使用一个没有定义的类或接口的情况下,它会自动被调用。

使用自动加载,在项目中你不必担心类的存放位置。定义一个良好定义的自动加载器,您不需要考虑一个类文件相对于当前类文件的位置,您只需使用类,自动加载器将自动查找文件。

此外,自动加载,确保只加载一次,提升了性能 -所以可以用它替代require_once()。

Zend Framework 鼓励使用自动加载,并提供了许多工具实现自动加载代码库以及应用程序代码。下面将介绍这些工具,以及如何有效地使用它们。

自动加载的实现约定

类命名约定

Zend Framework借鉴了 PEAR的想法,即类名与文件系统的1:1的关系。简单地说,下划线字符("_")替换目录分隔,以代表该文件的路径,然后添加后缀“.php”。例如,类“Foo_Bar_Baz”将对应文件系统上的"Foo/Bar/Baz.php"。假设已通过PHP的include_path设置类的位置,这使得可以通过 include() 和 require()找到相对include_path中设置的路径查找文件名。

此外,推荐使用供应商名称或项目名称作为前缀。这意味着,你写的所有的类都有一个共同的类前缀,例如,Zend Framework的所有代码前缀为“Zend_”。这种命名约定有助于防止命名冲突。在ZendFramework中,我们经常提到“namespace”前缀,要注意不要把它与PHP的本地命名空间混淆。

自动加载器设计约定

Zend Framework通过Zend_Loader_Autoloader实现支持自动加载的,主要提供有以下目标和设计元素:

提供命名空间匹配。如果类的命名空间前缀是没有注册的命名空间,会返回FALSE。

允许定义自动加载器作为一个备用的自动加载器。一个团队可能分布广泛,或使用一个为定义的命名空间前缀情况下,它会尝试匹配任何命名空间前缀。但是,这种做法是不推荐,因为它可能会导致不必要的查找。
允许开启禁止错误提示。 因此,默认情况下,它应该处于关闭状态。开发阶段,可以启用它。

可以自定义自动加载。一些开发商不希望使用Zend_Loader::loadClass()自动加载,但仍想使用Zend Framework的自动加载机制。 Zend_Loader_Autoloader允许使用自定义的自动加载。

允许使用SPL自动加载回调链。这样做的目的是允许指定额外的自动加载器 。

二、用法:

通常,只需将需要引入包含类,然后实例化它即可。由于Zend_Loader_Autoloader采用的单例模式,可以使用getInstance()方法来获取一个实例。

1

2

require_once 'Zend/Loader/Autoloader.php';

Zend_Loader_Autoloader::getInstance();

Copy after login

默认情况下,可以加载命名空间前缀为"Zend_"或 "ZendX_"的任何类,只要确保已经指定include_path。
如果想使用其他的命名空间前缀?最好的,最简单的方法是调用registerNamespace() 方法。您可以通过传递一个单一的命名空间前缀,或一个数组:

1

2

3

4

require_once 'Zend/Loader/Autoloader.php';

$loader = Zend_Loader_Autoloader::getInstance();

$loader->registerNamespace('Foo_');

$loader->registerNamespace(array('Foo_', 'Bar_'));

Copy after login

或者,你可以把Zend_Loader_Autoloader作为一个“备用”自动加载器。这意味着如果命名空间无论是否定义,都会尝试自动加载。

1

$loader->setFallbackAutoloader(true);

Copy after login

(注意:这种方式是不推荐的,尽量不要使用)。

Zend_Loader_Autoloader的内部实现是使用 Zend_Loader::loadClass() 加载类的。该方法的使用 include() 来尝试加载给定的类文件。 include()将返回一个布尔值,如果没有成功返回FALSE - ​​而且还发出PHP警告。 可能会导致以下问题:

如果启用了display_errors,警告将包含在输出中。

根据你所配置的error_reporting级别,它也可以输出到日志中。
可以禁止这些错误消息,具体如下:(但注意,display_errors启用时,错误日志将始终显示。)

1

$autoloader->suppressNotFoundWarnings(true);

Copy after login

选择一个Zend Framework的版本

ZendFramework/
|-- 1.9.2/
|   |-- library/
|-- ZendFramework-1.9.1-minimal/
|   |-- library/
|-- 1.8.4PL1/
|   |-- library/
|-- 1.8.4/
|   |-- library/
|-- ZendFramework-1.8.3/
|   |-- library/
|-- 1.7.8/
|   |-- library/
|-- 1.7.7/
|   |-- library/
|-- 1.7.6/
|   |-- library/

1

$autoloader->setZfPath($path, 'latest');

Copy after login

1

$autoloader->setZfPath($path, '1.8');

Copy after login

1

$autoloader->setZfPath($path, '1.7.7');

Copy after login

也可以使用配置文件

1

2

3

4

5

6

7

[production]

autoloaderZfPath = "path/to/ZendFramework"

autoloaderZfVersion = "1.7.7"

[qa]

autoloaderZfVersion = "1.8"

[development]

autoloaderZfVersion = "latest"

Copy after login

Autoloader接口

注:命名空间前缀和PHP命名空间

PHP5.3已经发布。该版本中,PHP现在已经正式支持命名空间。

然而,Zend Framework的命名空间和PHP 5.3的命名空间完全不同的。 Zend Framework中,提到的“命名空间”,是指一个类前缀。例如,所有的Zend Framework的类名称的前缀“Zend_”。 这是我们指定的“命名空间”。

在Zend Framework 2.0.0使用了原生的PHP命名空间。

自动加载器除了能够指定任意回调以外,Zend Framework还定义了一个需要自动加载类实现的接口Zend_Loader_Autoloader_Interface:

1

2

3

4

interface Zend_Loader_Autoloader_Interface

{

  public function autoload($class);

}

Copy after login

如果您希望在Zend Framework中使用自定义的自动加载器,可以使用 Zend_Loader_Autoloader的 pushAutoloader()和unshiftAutoloader()方法。
通过这些方法将在Zend Framework的内部自动装载器之后追加或之前使用自定义的加载器。

每个方法接受一个可选的第二个参数,类的命名空间前缀。自动加载器只查找给定的类前缀。如果不是指定的类前缀,将跳过自动加载器 , 这可能是一种性能改进方式。

当使用这个接口时,你需要传递类实例到Zend_Loader_Autoloader类的pushAutoloader()和unshiftAutoloader()方法,具体如下:

1

2

3

4

5

6

7

8

9

// Append function 'my_autoloader' to the stack,

// to manage classes with the prefix 'My_':

$loader->pushAutoloader('my_autoloader', 'My_');

// Prepend static method Foo_Loader::autoload() to the stack,

// to manage classes with the prefix 'Foo_':

$loader->unshiftAutoloader(array('Foo_Loader', 'autoload'), 'Foo_');

// Assume Foo_Autoloader implements Zend_Loader_Autoloader_Interface:

$foo = new Foo_Autoloader();

$autoloader->pushAutoloader($foo, 'Foo_');

Copy after login

Zend_Loader_Autoloader的相关方法

Method Return Value Parameters Description
getInstance() Zend_Loader_Autoloader N/A

获取实例

resetInstance() <font color="#000000" face="NSimsun">void</font> N/A

重置Zend_Loader_Autoloadersingleton实例的状态,恢复它的原始状态,注销所有的自动加载器回调和所有注册的命名空间。

autoload($class) <font color="#000000"><font face="NSimsun">string|<strong><tt>FALSE</tt></strong></font></font>
  • $class,required. A string class name to load.

试图加载一个类。

setDefaultAutoloader($callback) Zend_Loader_Autoloader
  • $callback,required.

指定默认的加载器回调

getDefaultAutoloader() <font color="#000000" face="NSimsun">callback</font> N/A

获取默认的加载器接口;默认是Zend_Loader::loadClass().

setAutoloaders(array $autoloaders) Zend_Loader_Autoloader
  • $autoloaders,required.

设置在自动加载器栈使用具体的自动加载器列表。自动加载器列表中的每个项目必须是PHPcallback。

getAutoloaders() Array N/A

 

getNamespaceAutoloaders($namespace) Array
  • $namespace,required

获取所有已注册的自动加载器来加载一个特定的的命名空间。

registerNamespace($namespace) Zend_Loader_Autoloader
  • $namespace,required.

注册命名空间. If$namespace is a string, it registers that namespace; if it's an array of strings, registers each as a namespace.

unregisterNamespace($namespace) Zend_Loader_Autoloader
  • $namespace,required.


getRegisteredNamespaces() Array N/A


suppressNotFoundWarnings($flag = null) <font color="#000000" face="NSimsun">boolean|Zend_Loader_Autoloader</font>
  • $flag,optional.

错误提示

setFallbackAutoloader($flag) Zend_Loader_Autoloader
  • $flag,required.

 

isFallbackAutoloader() Boolean N/A

 

getClassAutoloaders($class) Array
  • $class,required.

 

unshiftAutoloader($callback, $namespace = '') Zend_Loader_Autoloader
  • $callback,required. A valid PHPcallback

  • $namespace,optional. A string representing a class prefix namespace.

 

pushAutoloader($callback, $namespace = '') Zend_Loader_Autoloader
  • $callback,required. A valid PHPcallback

  • $namespace,optional. A string representing a class prefix namespace.

 

removeAutoloader($callback, $namespace = '') Zend_Loader_Autoloader
  • $callback,required. A valid PHPcallback

  • $namespace,optional. A string representing a class prefix namespace, or an array of namespace strings.


更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

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)

Tutorial on how to use Dewu Tutorial on how to use Dewu Mar 21, 2024 pm 01:40 PM

Dewu APP is currently a very popular brand shopping software, but most users do not know how to use the functions in Dewu APP. The most detailed usage tutorial guide is compiled below. Next is the Dewuduo that the editor brings to users. A summary of function usage tutorials. Interested users can come and take a look! Tutorial on how to use Dewu [2024-03-20] How to use Dewu installment purchase [2024-03-20] How to obtain Dewu coupons [2024-03-20] How to find Dewu manual customer service [2024-03-20] How to check the pickup code of Dewu [2024-03-20] Where to find Dewu purchase [2024-03-20] How to open Dewu VIP [2024-03-20] How to apply for return or exchange of Dewu

Quark browser usage tutorial Quark browser usage tutorial Feb 24, 2024 pm 04:10 PM

Quark Browser is a very popular multi-functional browser at the moment, but most friends don’t know how to use the functions in Quark Browser. The most commonly used functions and techniques will be sorted out below. Next, the editor will guide users. Here is a summary of the multi-functional usage tutorials of Quark Browser. Interested users can come and take a look together! Tutorial on how to use Quark Browser [2024-01-09]: How to scan test papers to see answers on Quark [2024-01-09]: How to enable adult mode on Quark Browser [2024-01-09]: How to delete used space on Quark [2024 -01-09]: How to clean up the Quark network disk storage space [2024-01-09]: How to cancel the backup of Quark [2024-01-09]: Quark

Upgrading numpy versions: a detailed and easy-to-follow guide Upgrading numpy versions: a detailed and easy-to-follow guide Feb 25, 2024 pm 11:39 PM

How to upgrade numpy version: Easy-to-follow tutorial, requires concrete code examples Introduction: NumPy is an important Python library used for scientific computing. It provides a powerful multidimensional array object and a series of related functions that can be used to perform efficient numerical operations. As new versions are released, newer features and bug fixes are constantly available to us. This article will describe how to upgrade your installed NumPy library to get the latest features and resolve known issues. Step 1: Check the current NumPy version at the beginning

In summer, you must try shooting a rainbow In summer, you must try shooting a rainbow Jul 21, 2024 pm 05:16 PM

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

What software is photoshopcs5? -photoshopcs5 usage tutorial What software is photoshopcs5? -photoshopcs5 usage tutorial Mar 19, 2024 am 09:04 AM

PhotoshopCS is the abbreviation of Photoshop Creative Suite. It is a software produced by Adobe and is widely used in graphic design and image processing. As a novice learning PS, let me explain to you today what software photoshopcs5 is and how to use photoshopcs5. 1. What software is photoshop cs5? Adobe Photoshop CS5 Extended is ideal for professionals in film, video and multimedia fields, graphic and web designers who use 3D and animation, and professionals in engineering and scientific fields. Render a 3D image and merge it into a 2D composite image. Edit videos easily

DisplayX (monitor testing software) tutorial DisplayX (monitor testing software) tutorial Mar 04, 2024 pm 04:00 PM

Testing a monitor when buying it is an essential part to avoid buying a damaged one. Today I will teach you how to use software to test the monitor. Method step 1. First, search and download the DisplayX software on this website, install it and open it, and you will see many detection methods provided to users. 2. The user clicks on the regular complete test. The first step is to test the brightness of the display. The user adjusts the display so that the boxes can be seen clearly. 3. Then click the mouse to enter the next link. If the monitor can distinguish each black and white area, it means the monitor is still good. 4. Click the left mouse button again, and you will see the grayscale test of the monitor. The smoother the color transition, the better the monitor. 5. In addition, in the displayx software we

Tutorial on how to turn off the payment sound on WeChat Tutorial on how to turn off the payment sound on WeChat Mar 26, 2024 am 08:30 AM

1. First open WeChat. 2. Click [+] in the upper right corner. 3. Click the QR code to collect payment. 4. Click the three small dots in the upper right corner. 5. Click to close the voice reminder for payment arrival.

Experts teach you! The Correct Way to Cut Long Pictures on Huawei Mobile Phones Experts teach you! The Correct Way to Cut Long Pictures on Huawei Mobile Phones Mar 22, 2024 pm 12:21 PM

With the continuous development of smart phones, the functions of mobile phones have become more and more powerful, among which the function of taking long pictures has become one of the important functions used by many users in daily life. Long screenshots can help users save a long web page, conversation record or picture at one time for easy viewing and sharing. Among many mobile phone brands, Huawei mobile phones are also one of the brands highly respected by users, and their function of cropping long pictures is also highly praised. This article will introduce you to the correct method of taking long pictures on Huawei mobile phones, as well as some expert tips to help you make better use of Huawei mobile phones.

See all articles