PHP: MVC

zhaozj2021-02-16  69

Recently, design model is popular, and MVC can be seen everywhere. The PHP field is no exception, and many forums have begun to discuss using MVC in PHP. However, how does M, V, and C do in PHP? Is MVC really suitable for PHP? Here, EASY gives some own thinking.

MVC prototype

The MVC is originally existing in the Desktop program, and m means a model, V refers to the user interface, and C is the controller. The purpose of using MVC is to separate the implementation code of M and V, so that the same program can use different forms of expression. For example, a batch of statistics can be expressed by column charts, pie charts, respectively. The purpose of the present is to ensure synchronization of M and V, once m changes, V should be updated synchronously.

MVC in Java

Java introduced MVC into the web field and on this basis, a set of systems called Model2. Due to the particularity of the Web, the MVC in the MVC and Desktop in Java is not exactly the same. The main reason is that V is not continuous in the web, and the user has access once. V will be regenerated once, so V is always consistent with M, no C is required to control synchronization. So what is C be doing in java? The C in Java is usually used for the steering of the process, and it is the DISPATCH mode, which is no longer C.

MVC in PHP

It is impossible to MVC to be COPY JAVA in PHP in PHP. The problem is mainly manifested on M, in Java, M is a data model independent of business logic and performance logic, and the server-side cross-page exists, Java Bean is playing this foot. The PHP process does not have a long time to stay in memory, only when the PHP page is executed, and it is over when the page interpretation is completed. In this case, we cannot directly implement M directly. Therefore, all PHP programs claimed to implement the MVC mode can only be implemented by simulation means. The implementation is usually stored in the database or cookie / session before the end of the current page, and the M is rebuilt through the database or cookie / session in the next page. In this way, the bean in Java can be said to be a huge overhead. The simple operation of writing data in memory is now transferred from the server to the client or database, then transfer it back. Instead of using such a big overhead to maintain a data model, then plugging this model back to the database, it is better to update the data inside the database directly as needed.

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

Then let's see V. Many PHP programs now use some template technology to write "using MVC architectures" in the description. In fact, most PHP template programs are only separated by HTML and PHP, rather than MVC emphasized display logic and business logic separation. A page mixing HTML and PHP can be part of the MVC. How to separate display logic and business logic has no satisfactory solution. I personally compare the implementation of http://www.phpe.net/articles/384.SHTML. As for C, and Java is similar, or it is used as a steering, which is not important in PHP.

An ultimate resource M a different distinguishing V can have no C to constitute a so-called MVC in PHP.

Based on the above analysis, EASY believes that the original 3-layer architecture of PHP is more efficient than the so-called MVC, and the performance layer in the 3-layer architecture is also solved in the MVC in the MVC. An improved 3-layer architecture is more suitable for PHP than MVC.

PS: Some options incorporate business logic into M (http://www.phpe.net/articles/382.shtml), which is actually the idea of ​​MVC.

Copyright statement: Easy original, repost, please keep sign, keep consistent, indicate the source.

转载请注明原文地址:https://www.9cbs.com/read-25641.html

New Post(0)