首页 > 开发 > PHP > 正文

Zend Framework教程之Loader以及PluginLoader用法详解

2024-05-04 22:32:06
字体:
来源:转载
供稿:网友

本文实例分析了Zend Framework中Loader以及PluginLoader用法。分享给大家供大家参考,具体如下:

Zend Framework提供了Zend_Loader,用来动态加载文件。

以下是具体用法,以及具体实现:

1.加载文件

使用方法:

Zend_Loader::loadFile($filename, $dirs=null, $once=false);

具体实现:

/** * Loads a PHP file. This is a wrapper for PHP's include() function. * * $filename must be the complete filename, including any * extension such as ".php". Note that a security check is performed that * does not permit extended characters in the filename. This method is * intended for loading Zend Framework files. * * If $dirs is a string or an array, it will search the directories * in the order supplied, and attempt to load the first matching file. * * If the file was not found in the $dirs, or if no $dirs were specified, * it will attempt to load it from PHP's include_path. * * If $once is TRUE, it will use include_once() instead of include(). * * @param string    $filename * @param string|array $dirs - OPTIONAL either a path or array of paths *            to search. * @param boolean    $once * @return boolean * @throws Zend_Exception */public static function loadFile($filename, $dirs = null, $once = false){  self::_securityCheck($filename);  /**   * Search in provided directories, as well as include_path   */  $incPath = false;  if (!empty($dirs) && (is_array($dirs) || is_string($dirs))) {    if (is_array($dirs)) {      $dirs = implode(PATH_SEPARATOR, $dirs);    }    $incPath = get_include_path();    set_include_path($dirs . PATH_SEPARATOR . $incPath);  }  /**   * Try finding for the plain filename in the include_path.   */  if ($once) {    include_once $filename;  } else {    include $filename;  }  /**   * If searching in directories, reset include_path   */  if ($incPath) {    set_include_path($incPath);  }  return true;}

参数规则:

正如实现方法,有如下参数

$filename参数指定需要加载的文件,注意$filename不需要指定任何路径,只需要文件名即可。ZF会对文件作安全性检查。$filename 只能由字母,数字,连接符-,下划线_及英文句号.组成(半角)。$dirs参数则不限,可以使用中文等。

$dirs 参数用来指定文件所在目录,可以是一个字符串或者数组。如果为 NULL,则程序将会到系统的 include_path 下寻找文件是否存在(include_path可在php.ini中设置--Haohappy注),如果是字符串或数组,则会到指定的目录下去找,然后才是 include_path。

$once 参数为布尔类型,如果为 TRUE,Zend_Loader::loadFile() 使用 PHP 函数 » include_once() 加载文件,否则就是 PHP 函数 » include()。(本参数只能是true或false,两者区别就和include()和include_once()的区别一样。)

2.加载类

具体使用:

Zend_Loader::loadClass('Container_Tree',  array(    '/home/production/mylib',    '/home/production/myapp'  ));            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表