PHP: The simplest way to implement MVC development, model thinking _ PHP Tutorial

Source: Internet
Author: User
PHP implements MVC development in the simplest way, model thinking. The simplest way to implement MVC in PHP is to develop the model. yesterday, some people in the group said that using MVC allowed the program to perform a lot of database operations, reducing the performance. this really surprised me. Mv php implements MVC development in the simplest way, model thinking

Yesterday, some people in the group said that the use of MVC allowed the program to operate a lot more databases, reducing the performance, which really surprised me. MVC is just a framework and has nothing to do with database operations. MVC only provides a clear programming and development mode. as long as you handle well, you cannot perform many unnecessary database operations. If an MVC allows a programmer to perform a lot of database operations without knowing it, it is definitely not a good MVC architecture. I think MVC only needs to provide a simple development framework, and there is no need to integrate many library classes. it is best to allow programmers to choose their own library classes.
The purpose of my own MVC framework is just to implement a simple MVC process. other users can add them as needed. Truly small, flexible, and efficient!
I wrote two articles in the last few weeks, PHP: The simplest way to implement MVC-View and template technology, and PHP: The simplest way to implement MVC-single point of entry. Today we will talk about how to implement the MVC model.
I have not studied the MVC theory in depth. for me personally, the model is a database encapsulation. you can obtain the corresponding data by calling the model method, but the implementation details do not need to be concerned by programmers. In actual development, it is very likely that a database table corresponds to a model. For example, a user information table userinfo corresponds to a model user. by calling the add () method of the model user, you can add a data record to the database by using select () you can implement query and update through update. At the same time, the model should be unrelated to the specific database type, whether you use mysql, oracle or SQL server. At the same time, I do not recommend using ROR in WEB development. it is very convenient and fast to use the SQL language for complex multi-table queries, and the performance is better. If a programmer has no knowledge of SQL, I don't think he is a qualified programmer. Therefore, I provide a query method in my model to implement direct SQL queries.
The following is a rough result of the model. Not the complete code. for the complete code, see the demo package.

  

In this model, I use arrays and database fields to correspond. Objects are used in early phpbeans. But later I felt that this method was not good in PHP and added a lot of unnecessary classes. It is more convenient to use arrays and better results (arrays in PHP are indeed a good thing, which is much better than JAVA ).

In the demo below, I used the mysql database for demonstration. the database operation class changes itself to an original database class. for details, see "modify previous database classes, php5-> php4.

The demo is described in detail below. Pai_^
In the index. php of the original package, add

 register('db',$mysql); unset($mysql); ?> 

This code is mainly used to register MYSQL into the register. for the usage principle of the register, see the two articles I have translated.
Create a new mysqlController. class. php file with the following code:

 Module = new module ('52site _ siteinfo'); // 52site_siteinfo indicates the table name $ this-> module-> query ("set names 'gb2312 '"); // If MYSQL5 is used, add this sentence} function indexAction () {print_r ($ this-> module-> select (); // read data}?>

First, add a model to the controller constructor. Then, call the model method in indexAction to display the data. In this way, the simplest query list is implemented. You can view your results through this address: http: // path/to/yoursite/mv...
In the future, I will write a specific demo to illustrate how to use other methods of the model, such as query, update, add, paging list, multi-table join query, and so on.

Articles you may be interested in
  • Php builds a simple example of its own MVC framework and provides ideas for your reference only
  • Simple example of php getting webpage content through socket
  • Some experiences in MVC development in PHP
  • PHP obtains the week of the specified date, the first day of the month and the last day of the month.
  • Use the PHP function memory_get_usage to obtain the current PHP memory consumption for program performance optimization.
  • Thinkphp development skills
  • Reasons why PHP adds a backslash before the quotation mark and how PHP removes the backslash, there are three ways to disable the php magic quotation mark
  • PHP retrieves the date list for the first 30 days of the current date

Yesterday, some people in the consumer group said that using MVC allowed the program to perform a lot of database operations, reducing the performance. this really surprised me. MV...

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.