<?php
set_include_path(get_include_path() . PATH_SEPARATOR . "core/main");
//set_include_path — Sets the include_path configuration option
function __autoload($object){
require_once("{$object}.php");
}
这个文件首先设置了include_path,也就是我们如果要找包含的文件,告诉系统在这个目录下查找。其实我们定义__autoload()方法,这个方法是在PHP5增加的,就是当我们实例化一个函数的时候,如果本文件没有,就会自动去加载文件。官方的解释是:
Many developers writing object-oriented applications create one PHP
source file per-class definition. One of the biggest annoyances is
having to write a long list of needed includes at the beginning of each
script (one for each class).
In PHP 5, this is no longer necessary. You may define an __autoload
function which is automatically called in case you are trying to use a
class/interface which hasn’t been defined yet. By calling this function
the scripting engine is given a last chance to load the class before PHP
fails with an error.
接下来我们看下面一句
initializer::initialize();
这就话就是调用initializer类的一个静态函数initialize,因为我们在ini.php,设置了include_path,以及定义了__autoload,所以程序会自动在core/main目录查找initializer.php.
initializer.php文件如下:
<?php
class initializer
{
public static function initialize(){
set_include_path(get_include_path().PATH_SEPARATOR . "core/main");
set_include_path(get_include_path().PATH_SEPARATOR . "core/main/cache");
set_include_path(get_include_path().PATH_SEPARATOR . "core/helpers");
set_include_path(get_include_path().PATH_SEPARATOR . "core/libraries");
set_include_path(get_include_path().PATH_SEPARATOR . "app/controllers");
set_include_path(get_include_path().PATH_SEPARATOR."app/models");
set_include_path(get_include_path().PATH_SEPARATOR."app/views");
//include_once("core/config/config.php");
}
}
?>
//user.php
<?php
class user
{
function base()
{
}
public function login()
{
echo 'login html page';
}
public function register()
{
echo 'register html page';
}
public function setParams($params){
var_dump($params);
}
}