Home  >  Article  >  Backend Development  >  Is MVC really suitable for PHP? _PHP Tutorial

Is MVC really suitable for PHP? _PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:25:27692browse

Design patterns have become popular recently, MVC can be seen everywhere, and the PHP field is no exception. Many forums have begun to discuss the use of MVC in PHP. However, how should M, V, and C be implemented in PHP? Is MVC really suitable for PHP? Here Easy gives some of its own thoughts.

The prototype of MVC

MVC originally existed in the Desktop program. M refers to the data model, V refers to the user interface, and C refers to the controller. The purpose of using MVC is to separate the implementation codes of M and V, so that the same program can use different representations. For example, you can represent a batch of statistical data in bar charts and pie charts respectively. The purpose of C is to ensure the synchronization of M and V. Once M changes, V should be updated synchronously.

MVC in JAVA

JAVA introduced MVC into the Web field, and based on this, a system called Model2 was built. Due to the particularity of the Web, MVC in JAVA and MVC in Desktop are not exactly the same. The main reason is that V in the Web is not persistent. Every time a user visits, V must be regenerated, so V is always consistent with M, and there is no need for C to control synchronization. So what is C in JAVA doing? The C in JAVA is usually used for process steering. What is actually used is the Dispatch mode, which is no longer the C in Desktop.

MVC in PHP

It is impossible to COPY JAVA’s MVC intact in PHP. The problem is mainly manifested in M. In JAVA, M is a data model independent of business logic and presentation logic. It exists across pages on the server side. JAVA Bean plays this role. The PHP process does not reside in memory for a long time. It is only created when the PHP page starts executing and ends when the page interpretation and execution ends. In such a case, we simply cannot implement M directly. Therefore, all PHP programs that claim to implement the MVC pattern can only implement M through simulation. The implementation method is generally to store the data in the database or cookie/session before the end of the current page, and then rebuild M through the database or cookie/session on the next page. Compared with beans in JAVA, this method can be said to be very expensive. The simple operation of writing data to the memory now needs to be transferred from the server to the client or database, and then back again. Instead of using such a large overhead to maintain a data model and then inserting the model back into the database at the end, it is better to directly update the data in the database as needed.

The MVC architecture in PHP can now be represented by the following figure.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446700.htmlTechArticleDesign patterns have become popular recently, MVC can be seen everywhere, and the PHP field is no exception. Many forums have begun to discuss its use in PHP MVC. However, how should M, V, and C be implemented in PHP? MVC is really suitable for...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn