Table of Contents
" >2. [代码]框架实现核心代码   
" >3. [代码]Home.controller.php  
4. [代码]Home.model.php     
" >5. [代码]Model模块调用的小小区别说明    
Home php教程 PHP源码 PHP Framework|PHP框架的简单实现|使用namepsace|单列模式运用

PHP Framework|PHP框架的简单实现|使用namepsace|单列模式运用

May 26, 2016 am 08:20 AM

跳至

//单入口文件index.php
<?php
require(&#39;config.php&#39;); //配置文件
require(&#39;system.php&#39;); //实现框架的核心类、方法

$uri = explode(&#39;/&#39;, $_SERVER[&#39;PATH_INFO&#39;]); //取得路由
spl_autoload_register(array(new \App\Web, &#39;autoload&#39;)); //注册自动加载(这里只加载常用的Library库)
\App\Web::lanuch($uri[1], $uri[2], array_slice($uri, 3)); //框架开始
Copy after login

2. [代码]框架实现核心代码

db = new \App\Lib\db;  //实例化mysql,因为考虑到web中,基本都会有CURD的操作,所以在框架开始的时候就把mysql的库实例化出来,后面就直接用就是了,当然要在后面按需加载也是可以的哟
		$this->load = new \App\Load; //实例一个工具类吧,后面在Controller里需要加载Model,Module等等时会用到
	}
	
	public function view($view){ //在Controller里使用的渲染视图时用到的
		$this->view = ucfirst($view);
		return self::$instance;		
	}
	
	public function data($data){  // 与上文中有view方法配合使用,传递array变量到视图
		extract($data);
		require(__DIR__.&#39;/View/&#39;.$this->view.&#39;.view.php&#39;);
	}
	
	public static function instance(){ //取得Controller的实例instance
		return self::$instance;
	}
		
}

class Model{
	
	public function __get($key){
		return	\App\Controller::instance()->$key;
	}  //__get魔术方法,比如在Model里面调用db的时候,就会通过这里取得在Controller已经实例好的那个mysql对象哟,不用再重复实例化了;
		
}

//===========================================================================================================//

class Load{  //相关加载的方法,这里没有做自动加载处理,如果有好的建议请讨论交流哟
	
	public function model($model){
		require(__DIR__.&#39;/Model/&#39;.ucfirst($model).&#39;.model.php&#39;);
	}
	
	public function cache($cache){
		require(__DIR__.&#39;/Cache/&#39;.ucfirst($cache).&#39;.cache.php&#39;);	
	}
	
	public function module($module){
		require(__DIR__.&#39;/Module/&#39;.ucfirst($module).&#39;.module.php&#39;);	
	}
	
	public function extend($extend){
		require(__DIR__.&#39;/Extend/&#39;.ucfirst($extend).&#39;.extend.php&#39;);	
	}

}
Copy after login

3. [代码]Home.controller.php

load->model(&#39;home&#39;); //加载home模块
		$model = new \App\Model\Home; //实例化
		$model->showHomeModel(); //调用 
            //  \App\Model\Home::showHomeModel();  也可以这样调用,但是会有小小的区别,会在最后说明
		$this->view(&#39;home&#39;)->data(array());
	}
		
	public static function test(){
		echo &#39;this is a test&#39;;
	}
	
}
Copy after login

4. [代码]Home.model.php

namespace App\Model;  //申明App空间下的Model子空间

class Home extends \App\Model{  //继承父层App空间下的Model类
	
	public function showHomeModel(){
		echo $this->db->query(&#39;use $this->db in home model
&#39;);
		echo &#39;this is home model function
&#39;;
	}
	
}
Copy after login

5. [代码]Model模块调用的小小区别说明

showHomeModel();
//在这里申明了要new一个Home.Model的对象实例,那么则会调用Home.Model继承的父类中的db实例,
//那么则就是App空间下的Model类的实例

//那么为会么会有这样的区别呢,是不是与static关键字有关呢,
//暂时还没有想得好明白,如果大家有什么建议,请分享出来哟;
Copy after login

           

       

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)