facebook twitter hatena line google mixi email

Php/autoload作成

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

サンプル

-AutoloadModel.php

class AutoloadModel
{
 public static function register()
 {
   spl_autoload_register(array(__CLASS__, 'load'));
 }
 public static function load($name)
 {
   if (preg_match("/(Dao|Logic)$/", $name, $matches)) {
     switch ($matches[1]) {
     case 'Dao':
       $path = APPLICATION_PATH . '/models/dao/' . $name . '.php';
       break;
     case 'Logic':
       $path = APPLICATION_PATH . '/models/logic/' . $name . '.php';
       break;
     }
     if (file_exists($path)) {
       require_once $path;
     }
   }
 }
}

-index.php

require_once APPLICATION_PATH . '/models/AutoloadModel.php';
AutoloadModel::register();