Archive for 2月 23rd, 2009

簡易クラスローダ

月曜日, 2月 23rd, 2009

車輪の再発明になっている可能性な気がしますが、PHP4で動作する、動的な簡易クラスローダを作成してみました。

大まかにはZend FrameworkのZend_Loaderを参考にしていますが、基本的に1から書き直していています。

PHP:
  1. <?php
  2. /**
  3. * 簡易クラスローダ
  4. * for PHP4
  5. *
  6. * @author anon <anon@anoncom.net>
  7. */
  8. class ClassLoader
  9. {
  10.     function ClassLoader() {}
  11.    
  12.     /**
  13.      * 指定されたクラスを読み込み、インスタンスを返します。
  14.      *
  15.      * @param string $class クラス名
  16.      * @param string|NULL クラスディレクトリ
  17.      * @param boolean $once requireでファイルを読み込む際に、require_onceで読み込むか否か
  18.      * @param array|NULL $param インスタンス生成時に引き渡す引数
  19.      * @param string|NULL $loaderMethod インスタンス生成時に呼び出される、コンストラクタ以外のメソッド名(Singletonパターンでの呼び出しの場合など)
  20.      */
  21.     function loadClass($class, $dir = NULL, $once = TRUE, $params = NULL, $loaderMethod = NULL)
  22.     {
  23.         if(is_null($dir)){
  24.             $dirs = explode(':', ini_get('include_path'));
  25.             $classfile_exists = false;
  26.             foreach($dirs as $dir){
  27.                 $filepath = $dir . '/' . $class . '.php';
  28.                 if(Froute_Loader::isFileAvailable($filepath)){
  29.                     $classfile_exists = true;
  30.                     break;
  31.                 }
  32.            
  33.             }
  34.             if(!$classfile_exists){
  35.                 trigger_error('could not load class, causes class file"' . $class . '" is not found.', E_USER_WARNING);
  36.             }
  37.         }else{
  38.             $filepath = $dir . '/' . $class . '.php';
  39.             if(!ClassLoader::isFileAvailable($filepath)){
  40.                 trigger_error('could not load class, causes class file "' . $filepath . '" is not found.', E_USER_ERROR);
  41.             }
  42.         }
  43.        
  44.         if($once){
  45.             require_once $filepath;
  46.         }else{
  47.             require $filepath;
  48.         }
  49.        
  50.         if(!class_exists($class)){
  51.             trigger_error('could not load class, causes undefined class name "' . $class . '".', E_USER_ERROR);
  52.         }
  53.        
  54.         if(!is_null($loaderMethod)){
  55.             if(!method_exists($loaderMethod)){
  56.                 trigger_error('could not load class, causes undefined function name "' . $class . '".', E_USER_ERROR);
  57.             }
  58.             if(is_null($params)){
  59.                 $instance = call_user_func(array($class, $loaderMethod));
  60.             }else{
  61.                 $instance = call_user_func(array($class, $loaderMethod), $params);
  62.             }
  63.         }else{
  64.             /*
  65.             if(!function_exists($class)){
  66.                 trigger_error('could not load class, \'causes undefined function name "' . $class . '".', E_USER_ERROR);
  67.             }
  68.             */
  69.             if(is_null($params)){
  70.                 $instance = new $class;
  71.             }else{
  72.                 //$instance = new call_user_func($class, $params);  // to use php5
  73.                 $instance = new $class($params);    // to use php4
  74.             }
  75.         }
  76.         return $instance;
  77.     }
  78.    
  79.     /**
  80.      * ファイルが存在するか確認します
  81.      *
  82.      * @param string $filename
  83.      * @return bool
  84.      */
  85.     function isFileAvailable($filename)
  86.     {
  87.         if(file_exists($filename) && is_readable($filename)){
  88.             return true;
  89.         }else{
  90.             return false;
  91.         }
  92.     }
  93. }



使い方は以下の通り
(続きを読む...)

Get Adobe Flash playerPlugin by wpburn.com wordpress themes