Recently, the design model has become popular, and MVC is everywhere. The PHP field is no exception. many forums have begun to discuss the application of MVC in PHP. However, how can M, V, and C be implemented in PHP? Is MVC really suitable for PHP? Here, Easy gives some thoughts. MVC originally
Recently, the design model has become popular, and MVC is everywhere. The PHP field is no exception. many forums have begun to discuss the application of MVC in PHP. However, how can M, V, and C be implemented in PHP? Is MVC really suitable for PHP? Here, Easy gives some thoughts.
MVC originally exists in the Desktop program, M is the exponential data model, V is the user interface, and C is the controller. The purpose of applying MVC is to separate the implementation code of M and V, so that the same program can apply different forms of performance. For example, you can use a column chart or pie chart to identify a batch of statistical data. The goal of C is to ensure the synchronization of M and V. once M changes, V should be updated synchronously.
MVC in JAVA
JAVA introduces MVC to the Web field, and builds a system called Model2 on this basis. Due to the special nature of the Web, MVC in JAVA and MVC in Desktop are not completely consistent. The important reason is that V in the Web is not continuous. every time a user visits a website, V is born again. Therefore, V is always consistent with M and does not require C to control synchronization. So what is C in JAVA? C in JAVA is usually used for turning the process. it actually uses the Dispatch mode and is no longer the C in Desktop.
MVC in PHP
It is impossible to copy java mvc in PHP. The title 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, and JAVA Bean plays this role. The PHP process does not reside in the memory for a long time. it is only created at the beginning of the PHP page and ended at the end of the page description. In this case, we basically cannot directly implement M. Therefore, all PHP programs that claim to implement the MVC mode can only implement M through imitation. Generally, data is stored in the database or cookie/session before the end of the current page, and M is rebuilt through the database or cookie/session on the next page. Comparing this method with the Bean in JAVA can be said to be costly. The simple control of writing data to the memory needs to be transferred from the server to the client or database, and then transmitted back. It is better to directly update the data in the database as needed to use such a large overhead to protect a data model and then plug the model back into the database.
Currently, the MVC architecture in PHP can be used for representation.
Then let's look at V. Now many PHP programs simply use some template techniques to write "using MVC architecture" in clarification ". Most PHP template programs are HTML and PHP respectively, rather than MVC exaggerated display logic and business logic. A page mixed with HTML and PHP can be a part of MVC. How to display the logic and business logic separately is not yet a satisfactory plan. I personally agree with the actual plan of http://www.phpe.net/articles/384.shtml. As for C, which is a few different from JAVA, it is still used as a redirection. it is not important to implement it in PHP.
A very resource-consuming M, a slightly different V, and a dispensable C constitute the so-called MVC in PHP.
Based on the above analysis, Easy believes that the original three-layer architecture of PHP is more efficient than the so-called MVC, and the V in the title MVC to be resolved in the three-layer architecture also exists. The improved three-layer architecture is more practical than MVC in PHP.