总有人间一两风,填我十万八千梦

PHP Yaf_Loader 用法 手册 | 示例代码

PHP开发手册 归档 371℃ 0评论

The Yaf_Loader class

(Yaf >=1.0.0)

简介

Yaf_Loader 类为Yaf提供了自动加载功能的全面解决方案。

在第一次使用的时候,将检索 Yaf_Application 的实例, Yaf_Loader 实现了单利模式,并使用spl_autoload注册它自己。 通过 Yaf_Loader::getInstance() 返回它的实例

Yaf_Loader 加载一个类时仅仅尝试一次,如果失败了, 后面的操作将取决于yaf.use_spl_auload, 如果这个配置项为On,Yaf_Loader::autoload() 将会返回FALSE, 从而把机会让给其他的自动加载功能。如果这个配置项为Off(默认), Yaf_Loader::autoload() 将会返回TRUE, 最重要的是将会抛出一个非常有用的警告(对于找出一个类加载失败非常有用)。

Note:

请保持yaf.use_spl_autoload保持关闭,除非有一些library有自己的autoload机制,并且是无法改写的。

默认情况下,Yaf_Loader 收集所有library(类定义的脚本)储存进在 php.ini(yaf.library)定义的global library directory之中。

如果你想使用 Yaf_Loader 搜索本地类(库)(定义在application.ini, 默认情况下,它是 application.directory . "/libraray"), 你需要使用 Yaf_Loader::registerLocalNameSpace() 注册本地类前缀。

让我们来看看一些例子(假设 APPLICATION_PATH 是 application.directory):

Example #1 Config example

// Assuming the following configure in php.ini: yaf.libraray = "/global_dir"  //Assuming the following configure in application.ini application.libraray = APPLICATION_PATH "/library"

假设以下本地名称空间已被注册:

Example #2 注册本地命名空间

<?php
class Bootstrap extends Yaf_Bootstrap_Abstract{
     public function 
_initLoader($dispatcher) {
          
Yaf_Loader::getInstance()->registerLocalNameSpace(array("Foo""Bar"));
     }
?>

自动加载例子:

Example #3 加载类

class Foo_Bar_Test =>   // APPLICATION_PATH/library/Foo/Bar/Test.php    class GLO_Name  =>   // /global_dir/Glo/Name.php   class BarNon_Test   // /global_dir/Barnon/Test.php

在PHP 5.3中,你可以使用命名空间:

Example #4 加载命名空间类

class FooBarDummy =>    // APPLICATION_PATH/library/Foo/Bar/Dummy.php  class FooBarBarDummy =>    // /global_dir/FooBar/Bar/Dummy.php

你可能会注意到所有文件夹名字的首字母是大写的,你可以通过在php.ini中设置 yaf.lowcase_path = On 来将它们小写。

Yaf_Loader 也是设计来加载MVC类,响应的规则如下:

Example #5 MVC类加载例子

Controller Classes => // APPLICATION_PATH/controllers/  Model Classes => // APPLICATION_PATH/models/  Plugin Classes => // APPLICATION_PATH/plugins/

Yaf 通过识别一个类的后缀(这个是默认的,你也可以通过改变配置项 yaf.name_suffix 来将它改为通过前缀识别)来决定它是否是一个MVC类:

Example #6 MVC 类区别

Controller Classes =>     // ***Controller  Model Classes =>     // ***Model  Plugin Classes =>     // ***Plugin

some examples:

Example #7 MVC loading example

class IndexController     // APPLICATION_PATH/controllers/Index.php  class DataModel =>    // APPLICATION_PATH/models/Data.php  class DummyPlugin =>   // APPLICATION_PATH/plugins/Dummy.php  class A_B_TestModel =>   // APPLICATION_PATH/models/A/B/Test.php

该目录将受 yaf.lowcase_path 的影响。

类摘要

Yaf_Loader {
/* 属性 */
protected $_local_ns ;
protected $_library ;
protected $_global_library ;
static $_instance ;
/* 方法 */
public autoload ( void ) : void
public clearLocalNamespace ( void ) : void
private __clone ( void ) : void
private__construct ( void )
public static getInstance ( void ) : void
public getLibraryPath ([ bool $is_global = false ] ) : Yaf_Loader
public getLocalNamespace ( void ) : void
public static import ( void ) : void
public isLocalName ( void ) : void
public registerLocalNamespace ([ mixed $prefix ] ) : void
public setLibraryPath ( string $directory [, bool $is_global = false ] ) : Yaf_Loader
private __sleep ( void ) : void
private __wakeup ( void ) : void

}

属性

_local_ns

_library

默认情况下,它的值是 application.directory . "/library", 你可以通过修改application.ini(application.library)或者调用 Yaf_Loader::setLibraryPath() 改变它。

_global_library

_instance

属性/方法 列表

用户贡献的笔记

There are no user contributed notes for this page.

转载请注明:悠然品鉴 » PHP Yaf_Loader 用法 手册 | 示例代码

喜欢 (0)or分享 (0)
发表我的评论
取消评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址