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' ) );

具体实现:

/** * Loads a class from a PHP file. The filename must be formatted * as "$class.php". * * 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 $dirs is null, it will split the class name at underscores to * generate a path hierarchy (e.g., "Zend_Example_Class" will map * to "Zend/Example/Class.php"). * * 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. * * @param string $class - The full class name of a Zend component. * @param string|array $dirs - OPTIONAL Either a path or an array of paths * to search. * @return void * @throws Zend_Exception */ public static function loadClass($class, $dirs = null) { if (class_exists($class, false) || interface_exists($class, false)) { return; } if ((null !== $dirs) && !is_string($dirs) && !is_array($dirs)) { require_once 'Zend/Exception.php'; throw new Zend_Exception('Directory argument must be a string or an array'); } // Autodiscover the path from the class name // Implementation is PHP namespace-aware, and based on // Framework Interop Group reference implementation: // $className = ltrim($class, '\\'); $file = ''; $namespace = ''; if ($lastNsPos = strripos($className, '\\')) { $namespace = substr($className, 0, $lastNsPos); $className = substr($className, $lastNsPos + 1); $file = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; } $file .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; if (!empty($dirs)) { // use the autodiscovered path $dirPath = dirname($file); if (is_string($dirs)) { $dirs = explode(PATH_SEPARATOR, $dirs); } foreach ($dirs as $key => $dir) { if ($dir == '.') { $dirs[$key] = $dirPath; } else { $dir = rtrim($dir, '\\/'); $dirs[$key] = $dir . DIRECTORY_SEPARATOR . $dirPath; } } $file = basename($file); self::loadFile($file, $dirs, true); } else { self::loadFile($file, null, true); } if (!class_exists($class, false) && !interface_exists($class, false)) { require_once 'Zend/Exception.php'; throw new Zend_Exception("File \"$file\" does not exist or class \"$class\" was not found in the file"); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/e2da8b06a6f57e035d1356041f6eff4c.html