技术开发 频道

用PHP在对象语境外创建载入程序

  【IT168 技术】我们将在本文中解释如何创建一个小型,高效的文件加载类,这其中有些与众不同的地方在于,由于实施了静态递归载入方法,我们不再需要产生大量实例来包括锁定文件。

  用PHP创建能够加载指定文件的小型文件或资源对于许多程序员来说都是很容易的一件事情。但是,当你尝试向这些项目中添加有用特性时事情会变得更复杂,如递归文件搜索功能和异常处理功能等。这些都要求更多对象范例的相关知识以及掌握目前仅出现在PHP5中的性能。

  文件加载类具备"load()"方法可以直接执行递归功能。然而,需要在此强调的是有必要创建给类的示例以便包含确定的文件,除非,在这些例子中,加载方法是被静态调用。通过php在服务器上写入文件

  为了避免加载类的最终示例,我们可以声明所提及的"load()"方法的静态特性。因此,考虑到这一点,我们将"Loader"类的特性进行改进。想了解这些是如何在简单几个步骤里实现的吗?请看下文。

  观察下列代码,注意它的定义:

 

  < ?php

  
// define a recursive loader class

  class Loader

  {

  
private $file = '';

  
private $path = '';

  
// constructor (not implemented)

  
public function __construct(){}

  
// set file to load

  
public function set_file($file)

  {

  $this
->file = $file;

  }

  
// get file to load

  
public function get_file()

  {

  return $this
->file;

  }

  
// set path to load file

  
public function set_path($path)

  {

  $this
->path = $path;

  }

  
// get path to load file

  
public function get_path()

  {

  return $this
->path;

  }

  
// load recursively specified file

  
public function load($file, $path)

  {

  
if (file_exists($file))

  {

  require_once($file);

  return;

  }

  
else

  {

  
if (is_dir($path))

  {

  
if (FALSE !== ($handle = opendir($path)))

  {

  
// search recursively the specified file

  
while (FAlSE !== ($dir = readdir($handle)))

  {

  
if (strpos($dir, '.') === FALSE)

  {

  $path .
= '/' . $dir;

  $file
= $path . '/' . $this->file;

  $this
->load($file, $path);

  }

  }

  }

  closedir($handle);

  }

  }

  }

  }

 

  如果你仔细看看上面的"Loader"类,就会意识到除了设置器和接收器以外,真正其作用的其实是"load()"方法。该方法不止是通过"require_once()"PHP函数包含指定文件,它还通过使用简单的贯穿网页服务器上正确目录的递归法则执行了这一任务。

  到目前为止,一切都很顺利。迅速查阅文件加载类的定义,下面,我们列入几个示例向大家展示如何包含两个基本的PHP文件:

 

  ('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"类的具体使用,使用每个示例中不同的路径来查找锁定文件。这是第一个示例:

 

  // create instance of Loader class

  $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

  
*/

 

  最后,看下第二个示例,它利用不同的创建路径来查找指定文件:

 

  // create instance of Loader class

  $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()"方法,让它只被对象语境外的情况调用是非常有帮助的。要做到这一点,只需要完善类的特性,使其看上去如下所示:

 

  class Loader

  {

  
// 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样本文件。请看示例:

 

  // example of usage of the recursive loader class

  
// 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()类实例。在这个具体示例中,笔者决定用虚构的创建路径来填充方法以便寻找这些文件,但是这里建议大家尝试用不同的路径并改变其位置,观察类会有什么动静,这样的尝试将极具启发性。

0
相关文章