The simplest way to implement MVC development in PHP--model _php techniques

Source: Internet
Author: User
Yesterday, some people in the group said that using MVC made the program a lot more database operations, which made the performance drop, which really took me by surprise. MVC is just a framework with no connection to database operations. MVC only provides a clear programming development model, as long as you deal with the good, there is no more than a lot of unnecessary database operations. An MVC is definitely not a good MVC architecture if it allows a programmer to make many database operations without knowing it. I think MVC simply provides a simple development framework, and there's no need to integrate a lot of library classes, and it's best to let programmers choose to use them.
The purpose of my own MVC framework is simply to implement a simple MVC process, and add to the specifics of the others. To achieve real small, flexible, efficient!
I wrote two articles in the last few weeks, the simplest way to implement MVC in PHP-View and template technology, and the simplest way to implement MVC in PHP-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, call the model method, you can get the appropriate data, but the implementation of the details of programmers do 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 have 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, through the update can be implemented. The model should be independent of the specific database type, regardless of the mysql,oracle or SQL Server you are using. At the same time, I don't recommend using ROR in Web development, how convenient and fast it is to use SQL language for complex, multiple-table queries, 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 Code code as follows:

?
Class module{

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

The table name corresponding to the Var $tbname;//model

Whether Var $debug =false;//is 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) {}//directly query SQL statements

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 specified record

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


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

In the demo below, I used a MySQL database to demonstrate that the database operation class to change the original library class, please see the "Modified Library class, PHP5->PHP4."

Below, detailed explanation uses the demo. ^_^
In the original package of the index.php inside the increase

?
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 to register inside, about the use of the Registrar principle, 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 achieve the most basic MVC function, does not include 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 for table name
$this->module->query ("Set names ' gb2312");/if it's MYSQL5 please add this sentence
}

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

The above first is the controller's constructor, which joins a model. Then call the model's method inside the indexaction to display the data. This enables the simplest list of queries. You can use this address to view your results http://path/to/yoursite/mv ...
Later I will write a specific demo to illustrate how to use the other methods of the model, such as query, update, add, paging list, multiple tables and so on.

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.