本文實例講述了PHP MVC框架中類的自動加載機制。分享給大家供大家參考,具體如下:
原文
實現(xiàn)類的自動加載主要使用到了set_include_path
和spl_autoload_register
函數(shù)。
set_include_path
用于提前設置好可能會加載的類的路徑。
spl_autoload_register
用于調用相關自動加載所需類的函數(shù),實現(xiàn)自動載入的功能。
有一點要注意的是:自動加載在實例化類的時候執(zhí)行,也就是說使用extends繼承類的時候,是不會自動加載父類的。
設置目錄如下:
實現(xiàn)自動加載功能相關的文件有:Loader.php,config.php,boot.php,index.php
config.php
1
2
3
4
5
6
7
8
9
10
11
|
<?php /** * Created by PhpStorm. * User: koastal * Date: 2016/5/15 * Time: 10:48 */ define( "APP_PATH" ,__DIR__. "/.." ); define( "Controller_PATH" ,__DIR__. "/../controller" ); define( "Model_PATH" ,__DIR__. "/../model" ); define( "View_PATH" ,__DIR__. "/../view" ); |
Loader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?php /** * Created by PhpStorm. * User: koastal * Date: 2016/5/15 * Time: 12:03 */ class Loader { public static function baseLoad() { require_once ( "Controller.php" ); require_once ( "Model.php" ); } public static function autoload( $class ) { $path = $class . ".class.php" ; require_once ( $path ); } } $include = array (Controller_PATH, Model_PATH,View_PATH); set_include_path(get_include_path() . PATH_SEPARATOR .implode(PATH_SEPARATOR, $include )); spl_autoload_register( array ( 'Loader' , 'autoload' )); Loader::baseLoad(); |
boot.php
1
2
3
4
5
6
7
8
|
<?php /** * Created by PhpStorm. * User: koastal * Date: 2016/5/15 * Time: 12:19 */ require_once ( "Loader.php" ); |
index.php
1
2
3
4
5
|
<?php require_once (__DIR__. "/libs/config.php" ); require_once (__DIR__. "/libs/boot.php" ); $obj = new testController(); $obj ->show(); |
經(jīng)測試,以上代碼可用,全文完。
加更
經(jīng)測試上面的代碼,在訪問不存在的控制器是會報錯,找不到相關類文件。因為我們缺少判斷相關類文件是否存在。因此,我們對Loader.php進行優(yōu)化,首先掃描相關類文件是否存在,如果不存在則報錯。
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
<?php /** * Created by PhpStorm. * User: koastal * Date: 2016/5/15 * Time: 12:03 */ require_once 'config.php' ; class Loader { public static function baseLoad() { require_once ( "Controller.php" ); require_once ( "Model.php" ); } public static function searchFile( $filename , $path ) { $filePath = false; $list = scandir( $path ); foreach ( $list as $file ){ $realPath = $path .DIRECTORY_SEPARATOR. $file ; if ( is_dir ( $realPath ) && $file != '.' && $file != '..' ){ $res = Loader::searchFile( $filename , $realPath ); if ( $res ){ return $res ; } } elseif ( $file != '.' && $file != '..' ){ if ( $file == $filename ){ $filePath = $realPath ; break ; } } } return $filePath ; } public static function autoload( $class ) { $filename = $class . ".class.php" ; $cflag = Loader::searchFile( $filename ,Controller_PATH); $mfalg = Loader::searchFile( $filename ,Model_PATH); $path = false; $path = ( $cflag != false)? $cflag : $path ; $path = ( $mfalg != false)? $mfalg : $path ; if ( $path == false){ exit ( "Class Load Failed." ); } else { require_once ( $path ); } } } Loader::baseLoad(); spl_autoload_register( array ( 'Loader' , 'autoload' )); |
希望本文所述對大家PHP程序設計有所幫助。
原文鏈接:https://blog.csdn.net/koastal/article/details/51417030