电子商城实录------定义init初始化的方法

路由方法的设置

//路由方法
private static function dispatch(){
//获取控制器名称(类比:英文单词的后缀)
$controller_name=CONTROLLER."Controller";
//获取方法名

$action_name=ACION."Action";

//实例化控制器对象
$controller=new $controller_name();
//调用方法
$controller->$action_name();


}

实现自动加载

自动加载联想到__autoload

面试题:__autoload是函数还是方法?

波利亚《怎样解题表》提出“回到定义中去”

函数的定义:普通函数,function来定义

方法的定义:类中定义的函数,和类挂钩。

即使在类中定义方法名字为__autoload,它也不能起到自动加载的作用

解决方案

1.写在类的外面

2.在类中实现

1) 写一个方法实现自动加载,如下:

//自动加载
private static function __autoload(){



}
//自动加载功能,实现控制器和数据库模型
//GoodsController GoodsModel
private static function load($classname){
if(substr($classname,-10)=="Controller"){

include CUR_CONTROLLER_PATH."{$classname}.class.php";
}elseif (substr($classname, -5)=='Model'){
//载入数据库模型
include MODEL_PATH."{$classname}.class.php";
}else{


}



}

第二步   类中注册方法

//自动加载功能,实现控制器和数据库模型
//GoodsController GoodsModel
private static function load($classname){
if(substr($classname,-10)=="Controller"){

include CUR_CONTROLLER_PATH."{$classname}.class.php";
}elseif (substr($classname, -5)=='Model'){
//载入数据库模型
include MODEL_PATH."{$classname}.class.php";
}else{


}



}

2)

//注册为自动加载
private static function autoload(){
$arr=array(__CLASS__,'load');
//__CLASS__获取当前的类名
spl_autoload_register($arr);


}

3)在run中调用

public static function run(){
//echo "hello,wrold!";
self::init();
self::autoload();
self::dispatch();

}

4)写一个控制器进行测试

 代码如下:

<?php
//后台首页控制器
class IndexController{

public function indexAction(){


echo "admin....index...";


}


}

?>

输出结果如下:  

http://localhost:8989/shopcz1/index.php

 

代表成功

整体代码如下:

Framework.class.php

<?php
//核心启动类
class Framework{
public static function run(){
//echo "hello,wrold!";
self::init();
self::autoload();
self::dispatch();

}
//初始化方法
private static function init(){

//定义路径常量
define("DS",DIRECTORY_SEPARATOR);
define("ROOT",getcwd().'/');
define("APP_PATH",ROOT.'application'.DS);
define("FRAMEWORK_PATH",ROOT.'framework'.DS);
define("PUBLIC_PATH",ROOT.'public'.DS);
define("GONFIG_PATH",APP_PATH."config".DS);
define("CONTROLLER_PATH",APP_PATH."controllers".DS);
define("MODEL_PATH",APP_PATH."models".DS);
define("VIEW_PATH",APP_PATH.'views'.DS);
define("CORE_PATH",FRAMEWORK_PATH.'core'.DS);
define("DB_PATH",FRAMEWORK_PATH.'databases'.DS);
define("LIB_PATH",FRAMEWORK_PATH.'libraries'.DS);
define("HELPER_PATH",FRAMEWORK_PATH.helpers);
define("UPLOAD_PATH",PUBLIC_PATH.'uploads'.DS);
//获取参数p ,c,a
define('PLATFORM', isset($_GET['p'])?$_GET['p']:"admin");
define('CONTROLLER',isset($_GET['c'])?ucfirst($_GET['c']):"Index");
define('ACTION',isset($_GET['a'])?$_GET['a']:"index");
//设置当前控制器和视图
define("CUR_CONTROLLER_PATH",CONTROLLER_PATH.PLATFORM.DS);
define("CUR_VIEW_PATH",VIEW_PATH.PLATFORM.DS);





}
//路由方法
private static function dispatch(){
//获取控制器名称
$controller_name=CONTROLLER."Controller";
//获取方法名

$action_name=ACTION."Action";

//实例化控制器对象
$controller=new $controller_name();
//调用方法
$controller->$action_name();


}
//注册为自动加载
private static function autoload(){
$arr=array(__CLASS__,'load');
//__CLASS__获取当前的类名
spl_autoload_register($arr);


}
//自动加载功能,实现控制器和数据库模型
//GoodsController GoodsModel
private static function load($classname){
if(substr($classname,-10)=="Controller"){

include CUR_CONTROLLER_PATH."{$classname}.class.php";
}elseif (substr($classname, -5)=='Model'){
//载入数据库模型
include MODEL_PATH."{$classname}.class.php";
}else{


}



}
}

?>

测试代码:

IndexController.class.php

<?php
//后台首页控制器
class IndexController{

public function indexAction(){


echo "admin....index...";


}


}

?>

原文地址:https://www.cnblogs.com/zhouxingzhi/p/10306875.html