The project directory is as follows:
1. Put the classes that need to be registered in an array
Copy code The code is as follows:
final class Utils {
private function __construct() {
}
public static function getClasses($pre_path = '/') {
$classes = array(
'DBConfig' => $pre_path.'DBConfig/DBConfig.php ',
' user '= & gt; $ pre_path.'model/user.php',
'dao' = & gt; $ pre_path.'dao/dao.php ',
' userdao '= & gt ; $pre_path.'Dao/UserDao.php',
'UserMapper' => >}
?>
2. Registration array
Note:
The class paths in step 1 are all relative to init .php, not relative to Utils, this is because we require the class
through the automatic loading function spl_autoload_register in init.php. Copy code
The code is as follows:require_once '/Utils/Utils.php';
final class Init {
/**
* System config.
*/
public function init() {
// reporting error - 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->init ();
?>
3. In this example, require init.php
Copy code
The code is as follows:
require_once 'Init.php';$dao = new UserDao();$result = $dao->findByName('zcl');?>
http://www.bkjia.com/PHPjc/327995.html
www.bkjia.comtrue
http: //www.bkjia.com/PHPjc/327995.htmlTechArticleThe project directory is as follows: 1. Place the classes that need to be registered in an array and copy the code as follows: ?php final class Utils { private function __construct() { } public static function g...