工程目录如下:
1、将需要注册的类放在一个数组中
复制代码 代码如下:
<?php
final class Utils {
private function __construct() {
}
public static function getClasses($pre_path = '/') {
$classes = array(
'DBConfig' => $pre_path.'DBConfig/DBConfig.php',
'User' => $pre_path.'Model/User.php',
'Dao' => $pre_path.'Dao/Dao.php',
'UserDao' => $pre_path.'Dao/UserDao.php',
'UserMapper' => $pre_path.'Mapping/UserMapper.php',
);
return $classes;
}
}
?>
2、注册数组
注意:步骤1中的类的路径都是相对于init.php而言的,不是相对于Utils而言的,这是因为我们通过init.php里的自动加载函数spl_autoload_register来require类的
复制代码 代码如下:
<?php
require_once '/Utils/Utils.php';
final class Init {
/**
* System config.
*/
public function init() {
// error reporting - all errors for development (ensure you have
// display_errors = On in your php.ini file)
error_reporting ( E_ALL | E_STRICT );
mb_internal_encoding ( 'UTF-8' );
//registe classes
spl_autoload_register ( array ($this,'loadClass' ) );
}
/**
* Class loader.
*/
public function loadClass($name) {
$classes = Utils::getClasses ();
if (! array_key_exists ( $name, $classes )) {
die ( 'Class "' . $name . '" not found.' );
}
require_once $classes [$name];
}
}
$init = new Init ();
$init->init ();
?>
3、本例中在使用处test.php里require init.php
复制代码 代码如下:
<?php
require_once 'Init.php';
$dao = new UserDao();
$result = $dao->findByName('zcl');
?>
类,注册,自动加载
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 华少翌.2005-华少翌【中唱艺能】【WAV+CUE】
- 群星《十二女声Ⅱ》[WAV/CUE/分轨][527MB]
- 大自然音乐系列《漫步在山裡》[WAV/CUE/分轨][502.8MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]
- 史依弘《史依弘交响乐京剧演唱专辑HQ》头版限量编号[WAV+CUE]
- 曼丽2020《女人三十风继续吹》HQCD[WAV+CUE]
- 曼丽2021《女人三十2雾之恋》HQCD[WAV+CUE]
- 隔壁老樊.2019-我曾【好听音乐】【FLAC分轨】
- 高林生.1993-牵挂你的人是我【白天鹅】【WAV+CUE】
- 洪卓立.2012-找个懂我的女孩(国专)【英皇娱乐】【WAV+CUE】
- 曼丽2022《女人三十3晚秋》HQCD[WAV+CUE]
- 曼丽2018《女人三十4冬天》HQCD[WAV+CUE]
- 柏菲-《好歌30年壹》限量开盘母带ORMCD[低速原抓WAV+CUE]
- 大自然音乐系列《水舞》[WAV/CUE/分轨][424.3MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]