Is MVC really suitable for PHP?

Source: Internet
Author: User
Tags php template
Recently, the design pattern has become popular. MVC is everywhere, and the PHP field is no exception. many forums have begun to discuss how to use 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 prototype MVC was originally existed... "> <LINKhref =" http://www.php100.com//sta

Recently, the design pattern has become popular. MVC is everywhere, and the PHP field is no exception. many forums have begun to discuss how to use 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 prototype

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 using MVC is to separate the implementation code of M and V, so that the same program can use different expressions. For example, you can use a column chart or pie chart to represent a batch of statistical data. The purpose of C is to ensure synchronization between 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 exactly the same. The main reason is that V in the Web is not continuous. every time a user accesses it, V must be generated 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. In fact, it uses the Dispatch mode, instead of C in Desktop.

MVC in PHP

It is impossible to copy java mvc in PHP. The problem is mainly manifested in M. in JAVA, M is a data model independent of business logic and presentation logic, and exists across pages on the server side. JAVA Bean plays this role. The PHP process does not reside in the memory for a long time. it is created only when the PHP page starts to be executed and ended when the page interpretation execution ends. In this case, we cannot directly implement M. Therefore, all PHP programs that claim to implement the MVC mode can only implement M through simulation. 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. This method is similar to the Bean in JAVA, which is costly. The simple operation of writing data to the memory needs to be uploaded 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 maintain a data model with such a large overhead 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 technologies to write "using MVC architecture" in the description ". In fact, the vast majority of PHP template programs only separate HTML and PHP, rather than the display logic and business logic separation emphasized by MVC. A page mixed with HTML and PHP can be a part of MVC. There is no satisfactory solution to separate the display logic from the business logic. I personally agree with the actual solution of http://www.phpe.net/articles/384.shtml. As for C, similar to JAVA, it is still used for steering. this is not important in PHP.

A very resource-consuming M + A slightly different V + dispensable C constitutes 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 problem that the presentation layer of the three-layer architecture should solve in the same way in mvc v. The improved layer-3 architecture is more suitable for PHP than MVC.

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.