PHP Framework|PHP框架的简单实现|使用namepsace|单列模式运用
//单入口文件index.php <?php require('config.php'); //配置文件 require('system.php'); //实现框架的核心类、方法 $uri = explode('/', $_SERVER['PATH_INFO']); //取得路由 spl_autoload_register(array(new \App\Web, 'autoload')); //注册自动加载(这里只加载常用的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__.'/View/'.$this->view.'.view.php'); } 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__.'/Model/'.ucfirst($model).'.model.php'); } public function cache($cache){ require(__DIR__.'/Cache/'.ucfirst($cache).'.cache.php'); } public function module($module){ require(__DIR__.'/Module/'.ucfirst($module).'.module.php'); } public function extend($extend){ require(__DIR__.'/Extend/'.ucfirst($extend).'.extend.php'); } }
Copy after login
3. [代码]Home.controller.php
load->model('home'); //加载home模块 $model = new \App\Model\Home; //实例化 $model->showHomeModel(); //调用 // \App\Model\Home::showHomeModel(); 也可以这样调用,但是会有小小的区别,会在最后说明 $this->view('home')->data(array()); } public static function test(){ echo 'this is a test'; } }
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('use $this->db in home model '); echo 'this is home model function '; } }
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
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
Assassin's Creed Shadows: Seashell Riddle Solution
3 weeks ago
By DDD
What's New in Windows 11 KB5054979 & How to Fix Update Issues
2 weeks ago
By DDD
Where to find the Crane Control Keycard in Atomfall
3 weeks ago
By DDD
Saving in R.E.P.O. Explained (And Save Files)
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics
CakePHP Tutorial
1386
52

