用PHP为加载程序开发一个递归加载类
在我们创建具备递归搜索功能的文件加载类前,先看一下如何创建加载类,下面的代码显示了Loader类的特性:
// define the loader class (the 'load()' method is declared static)
class Loader
{
// constructor (not implemented)
public function __construct(){}
// load specified file
public static function load($filepath)
{
if (!file_exists($filepath))
{
throw new Exception('The specified file cannot be found!');
}
require_once($filepath);
}
}
下面,我们要介绍两个将被该类包含的文件样本
('sample_file1.php')
< ?php
echo ' This file has been loaded with the Loader class.' . '< br />';
?>
('sample_file2.php')
< ?php
echo 'This file has been loaded at the following time: ' . date('H:i:s');
?>
最后,这里是一段小型代码,它显示了正在运转的Loader类,该类使用的是其静态load()方法:
< ?php
try
{
// call 'load()' method statically and load specified files
Loader::load('sample_file1.php');
Loader::load('sample_file2.php');
/* displays the following
This file has been loaded with the Loader class.
This file has been loaded at the following time 11:37:09
*/
}
catch (Exception $e)
{
echo $e->getMessage();
exit();
}
?>