根据前一篇文章的设计原则,我们暂时把php文件分为3类,类名和文件名都遵守如下约定。
类名 | 文件名 | 路径 | |
模型类m | {$app}Mod | {$app}.mod.php | {$app}/model |
控制器类c | {$app}Ctl | {$app}.ctl.php | {$app}/control |
其他 | {$app} | {$app}.cls.php | {$app}/class |
可以实现一个简单的autoload函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
function uct_autoload( $class_name ) {
$auto_path = array (
'ctl' => 'control' ,
'mod' => 'model' ,
'cls' => 'class' ,
);
$key = strtolower ( substr ( $class_name , -3));
if (isset( $auto_path [ $key ])) {
$dir = $auto_path [ $key ] . DS . strtolower ( substr ( $class_name , 0, -3)) . '.' . $key . '.php' ;
} else {
$dir = 'class' . DS . strtolower ( $class_name ) . '.cls.php' ;
}
if (! empty ( $GLOBALS [ '_UCT' ][ 'autoload' ])) {
foreach ( $GLOBALS [ '_UCT' ][ 'autoload' ] as $app ) {
if ( file_exists (UCT_PATH . 'app' . DS . $app . DS . $dir )) {
return include UCT_PATH . 'app' . DS . $app . DS . $dir ;
}
}
}
if ( file_exists (UCT_PATH . 'framework' . DS . $dir )) {
return include UCT_PATH . 'framework' . DS . $dir ;
}
echo 'auto_load not found! ' . $class_name ;
exit (1);
} |
如果想使用另一个模块里的函数可以使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
function uct_use_app( $app ) {
if ( empty ( $GLOBALS [ '_UCT' ][ 'autoload' ])) {
$GLOBALS [ '_UCT' ][ 'autoload' ] = array (
$app
);
return true;
}
if (!in_array( $app , $GLOBALS [ '_UCT' ][ 'autoload' ])) {
array_unshift ( $GLOBALS [ '_UCT' ][ 'autoload' ], $app );
return true;
}
return false;
} |