PHP's simplest way to implement MVC--model _php Tutorial

Source: Internet
Author: User
Yesterday, some people said that using MVC to make the program a lot more database operations, resulting in performance degradation, it really surprised me. MVC is just a framework that has no relation to database operations. MVC just provides a clear pattern of programming development, and as long as you handle it well, it is impossible to do many unnecessary database operations. An MVC is definitely not a good MVC architecture if it allows a programmer to do a lot more database operations without knowing it. I think MVC just provides a simple development framework, it is not necessary to integrate many library classes, library classes are best to let the programmer choose to use.
My own purpose of this MVC framework is simply to implement a simple MVC process that other people specifically add. To be truly small, flexible, efficient!
I wrote two articles in the last few weeks, the simplest way for PHP to develop MVC--view and template technology, the simplest way for PHP to develop MVC--a single point of entry. Today, specifically, how to implement the MVC model.
I did not delve into the theory of MVC, for me personally, the model is a database encapsulation, the method of invoking the model, you can get the corresponding data, but the implementation of the details of the programmer does not need to care. In actual development, it is likely that a database table corresponds to a model. For example, a user information table userinfo, corresponding to a model user, by invoking the model user's Add () method you can add a data to the database, through select () you can implement the query, update can be implemented by updating. The model should also be independent of the specific database type, whether you are using mysql,oracle or SQL Server. At the same time, I do not recommend the use of ROR in web development, complex multi-table query using SQL language is how convenient and fast things, and better performance. If a programmer doesn't have the knowledge of SQL, I don't think he is a qualified programmer. So, in my model, I provide a query method to implement a direct SQL query.
Here is an approximate result of the model. Not complete code, please see Demo package for complete code.

Copy CodeThe code is as follows:
Class module{

var $mysql;//Database operation class, can be mysql,oracle,sql, etc.

var $tbname;//table name corresponding to the model

var $debug whether =false;//is a debug mode

function Module ($tbname, $db = ") {}//constructor

function _setdebug ($debug =true) {}//turn debug mode on or off

function Add ($row, $tbname = ') {}//adds a new record

function query ($strsql) {}//query SQL statement directly

function count ($where = ', $tbname = ') {}//count statistics

function Select ($where = ', $tbname = ') {}//query

function Delete ($where = ', $tbname = ') {}//Delete a record that satisfies the condition

function Update ($set, $where, $tbname = ') {}//update the specified record

function Detail ($where, $tbname = ") {}//displays a record in detail
}
?>


In this model, I use arrays and database fields to correspond. The early Phpbean used objects to correspond. But later it felt that this method was not good in PHP, and added a lot of meaningless classes. Using arrays is more convenient and works better (arrays in PHP are really good things, which is a lot better than Java).

In the following demo, I used the MySQL database to demonstrate, where the database operation class changed itself to a library class, in detail, see the "Modified Library class, PHP5->PHP4".

Below, the detailed explanation uses the demo. ^_^
Added to the index.php of the original package

Require_once (Site_path. ' /libs/phpbean.class.php ');
Require_once (Site_path. ' /libs/mysql.class.php ');
$phpbean =new Phpbean ();
Global $phpbean;

$mysql =new MySQL ("localhost", "* * * *", "* * *", "52site");
$phpbean->register (' db ', $mysql);
Unset ($mysql);
?>

This code is mainly to register MySQL into the registrar, about the use of the principle of the Registrar, you can see my translation of the two articles.
Then create a new mysqlController.class.php file with the following code:

/**
* MVC Demo Demo
* Only the most basic MVC functions are implemented, not including security processing, data filtering, and other optimization measures.
* @author: FEIFENGXLQ
* @since: 2007-1-24
* @copyright http://www.phpobject.net/blog/
*/
Class Mysqlcontroller
{
var $module;

function Mysqlcontroller () {
Require_once (Site_path. ' /libs/module.class.php ');
$this->module=new module (' 52site_siteinfo ');//52site_siteinfo as the table name
$this->module->query ("Set names ' gb2312 '");//If it is MYSQL5, please add this sentence.
}

function Indexaction () {
Print_r ($this->module->select ());//This enables reading of data
}
}
?>

Above first is the Controller's constructor, adding a model. It then invokes the model's method in the Indexaction to display the data. This allows the simplest query list to be implemented. You can check your results by this address http://path/to/yoursite/mv ...
Later I will write a specific demo to illustrate how to use the model of other methods, such as query, update, add, page list, multi-table connection and so on.

http://www.bkjia.com/PHPjc/318188.html www.bkjia.com true http://www.bkjia.com/PHPjc/318188.html techarticle yesterday, some people said that using MVC to make the program a lot more database operations, resulting in performance degradation, it really surprised me. MVC is just a framework that has no relation to database operations. ...

  • 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.