PHPFramework | simple PHP framework implementation | use namepsace | single-column mode application-PHP source code

Source: Internet
Author: User
PHPFramework | simple implementation of the PHP framework | use namepsace | skip

// Single-entry file index. php
 

2.[Code]Core code of framework implementation
Db = new \ App \ Lib \ db; // instantiate mysql, because in the web, there are basically CURD operations, so at the beginning of the framework, I instantiate the mysql database and use it directly later, of course, you can load it as needed. $ this-> Load = new \ App \ load; // A tool class for the instance. you need to Load the Model in the Controller later, module and so on will use} public function view ($ view) {// $ this-> view = ucfirst ($ view) used in rendering view in Controller ); return self: $ instance;} public function data ($ data) {// used with the view method in the preceding section to pass the array variable to the view extract ($ data ); require (_ DIR __. '/View /'. $ this-> view. '. view. php ');} public static function instance () {// get the Controller instance instancereturn self: $ instance ;}} class Model {public function _ get ($ key) {return \ App \ Controller: instance ()-> $ key;} // _ get magic method, for example, when db is called in the Model, the mysql object that has been used in the Controller instance will be obtained here. you do not need to instantiate it again ;} // ================================================ ========================================================== ==========================================/// class Load {// related loading methods, automatic loading is not performed here. if you have good suggestions, please discuss them. 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 ');}}

3.[Code]Home. controller. php
Load-> model ('Home'); // load the home module $ model = new \ App \ Model \ home; // instantiate $ model-> showHomeModel (); // call // \ App \ Model \ Home: showHomeModel (); you can also call this method, but there is a small difference, $ this-> view ('Home')-> data (array ();} public static function test () {echo 'This is a test ';}}

4.[Code]Home. model. php
Namespace App \ Model; // declare the Model sub-space class Home extends \ App \ Model in the App space {// inherit the Model class public function showHomeModel () in the parent App space () {echo $ this-> db-> query ('use $ this-> db in home Model'); echo 'this is home model function ';}}

5.[Code]Differences between Model module calls
ShowHomeModel (); // declare a new Home here. the object instance of the Model will call Home. the db instance in the parent class inherited by the Model, // is the instance of the Model class in the App space, is it related to the static keyword? // I haven't figured it out yet. if you have any suggestions, please share them with me;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.