Last active
August 29, 2015 13:56
-
-
Save sushat4692/8799311 to your computer and use it in GitHub Desktop.
spl_autoload_register
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// abstract class Object { /* ... */ } | |
class Loader extends Object { | |
/** | |
* Class_action という命名規則と仮定 | |
* action -> model, controller それ以外は汎用クラスと解釈 | |
* ex) Post_controller, Category_model | |
*/ | |
public static function load_class( $class ) { | |
if( preg_match( '/(.*)_(.*)$/', $class, $matches ) ) { | |
$class_name = $matches[1]; | |
$action_name = $matches[2]; | |
switch( $action_name ) { | |
case 'model': | |
self::get_model( $class_name ); | |
break; | |
case 'controller': | |
self::get_controller( $class_name ); | |
break; | |
default: | |
self::get_class( $class ); | |
break; | |
} | |
} else { | |
self::get_class( $class ); | |
} | |
} | |
public static function get_model( $class_name ) { | |
// Model Classをrequireする処理をゴニョゴニョする | |
} | |
public static function get_controller( $class_name ) { | |
// Controller Classをrequireする処理をゴニョゴニョする | |
} | |
public static function get_class( $class ) { | |
// 汎用Classをrequireする処理をゴニョゴニョする | |
} | |
} | |
spl_autoload_register( array( 'Loader', 'load_class' ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment