到目前为止,一切都很顺利。迅速查阅文件加载类的定义,下面,我们列入几个示例向大家展示如何包含两个基本的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"类的具体使用,使用每个示例中不同的路径来查找锁定文件。这是第一个示例:
$loader = new Loader();
// set file to load
$loader->set_file('sample_file1.php');
// set path of specified file
$loader->set_path($_SERVER['DOCUMENT_ROOT'] . '/folder1);
// try to load specified file
$loader->load($loader->get_file(), $loader->get_path());
// set another file to load
$loader->set_file('sample_file2.php');
// try to load specified file
$loader->load($loader->get_file(), $loader->get_path());
/* displays the following
This file has been loaded with the Loader class.
This file has been loaded at the following time 10:32:51
*/
最后,看下第二个示例,它利用不同的创建路径来查找指定文件:
$loader = new Loader();
// set file to load
$loader->set_file('sample_file1.php');
// set path of specified file
$loader->set_path($_SERVER['DOCUMENT_ROOT']);
// try to load specified file
$loader->load($loader->get_file(), $loader->get_path());
// set another file to load
$loader->set_file('sample_file2.php');
// try to load specified file
$loader->load($loader->get_file(), $loader->get_path());
/* displays the following
This file has been loaded with the Loader class.
This file has been loaded at the following time 10:45:02
*/
上面的代码示例应该让大家明白了文件加载类到底是怎么一回事。但是,如我们在引言中提到的,这样做有必要生成一个类实例来包含锁定文件,而这种麻烦完全可以通过声明其"load()"方法的静态特性来避免。
改进Loader类的定义
所以,基于这一概念,我们要通过修改前面讨论过的方法对"Loader()"类的定义加以改进,并且要为其纳入基本的错误处理机制。如果我们在前面所解释的,声明之前文件加载类静态的"load()"方法,让它只被对象语境外的情况调用是非常有帮助的。要做到这一点,只需要完善类的特性,使其看上去如下所示:
{
// constructor (not implemented)
public function __construct(){}
// load recursively a specified file
public static function load($file, $path)
{
$filepath = $path . '/' . $file;
if (file_exists($filepath))
{
require_once($filepath);
}
else
{
if (!is_dir($path))
{
throw new Exception('The supplied path is invalid!.');
}
if (FALSE !== ($handle = opendir($path)))
{
// search recursively the specified file
while (FAlSE !== ($dir = readdir($handle)))
{
if (strpos($dir, '.') === FALSE)
{
$path .= '/' . $dir;
self::load($file, $path);
}
}
closedir($handle);
}
}
throw new Exception('The specified file was not found!.');
}
}
正如你所看到的,不仅是设置器和接收器被移出了"Loader"类,"load()"方法也因为我们之前所给的理由而被声明为静态。此外,要注意另外一个被引入该方法的精妙改进:现在如果在提供的路径上找不到包含的文件,它会抛出一个常规异常。
毫无疑问,加载器类现在的功能更强大,最重要的是,它可以用来动态包含文件而不需要处理其实例。因此,下一步就让我们看看在它实际情况中的效果。
类加载器
当然,演示增强版类加载器工作原理的最好方法是学习具体示例。因此我们为大家带来了一段易于掌握的代码段,该代码使用的是类的静态"load()"方法以便包含文章开头大家看到的两个PHP样本文件。请看示例:
// prepare path to search for the specified file
$path = $_SERVER['DOCUMENT_ROOT'] . '/path';
// try to load the first specified file
Loader::load('sample_file1.php', $path);
// try to load the second specified file
Loader::load('sample_file2.php', $path);
/* displays the following
This file has been loaded with the Loader class.
This file has been loaded at the following time 22:18:21
*/
这段代码易于编写和读取。正如大家所看到的,现在样本文件已经被成功加入到上述脚本中,但是没有产生不必要的Loader()类实例。在这个具体示例中,笔者决定用虚构的创建路径来填充方法以便寻找这些文件,但是这里建议大家尝试用不同的路径并改变其位置,观察类会有什么动静,这样的尝试将极具启发性。