PHPFramework | simple implementation of the PHP framework | use namepsace | skip
// Single-entry file index. php
2.[Code]Core code of framework implementationDb = 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. phpLoad-> 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. phpNamespace 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 callsShowHomeModel (); // 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;