PHP implements the simplest method of MVC development, model thinking _php tutorial

Source: Internet
Author: User

PHP implements the simplest method of MVC development, model thinking


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.

 
  

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

 
  

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:

 
  Module=new module (' 52site_siteinfo ');//52site_siteinfo is the table name $this->module->query ("Set names ' gb2312 '");// If it is MYSQL5 please add this} function Indexaction () {Print_r ($this->module->select ());//This implements read 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.

Articles you may be interested in

    • PHP builds its own simple case of MVC framework, providing ideas for reference only
    • Simple example of PHP getting Web content via socket
    • A little bit of the development of MVC in PHP
    • PHP gets the week of the specified date, the first day of the month, and the last day
    • Use PHP function memory_get_usage to get the current PHP memory consumption to achieve program performance optimization
    • Summary of thinkphp development skills
    • PHP to add a backslash in front of the reason and PHP to remove the backslash method, three ways to close the PHP magic quotes
    • PHP gets a list of dates 30 days before the current date

http://www.bkjia.com/PHPjc/976852.html www.bkjia.com true http://www.bkjia.com/PHPjc/976852.html techarticle PHP Implementation of MVC development of the simplest method, the model of thinking yesterday, some people say that using MVC to make the program a lot more database operations, so that performance degradation, this really let me take a surprise. MV ...

  • Related Article

    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.