Is MVC really a good fit for PHP?

Source: Internet
Author: User
Tags php template

Recently, design patterns are popular, MVC is ubiquitous, and the PHP field is no exception, and many forums are beginning to discuss the use of MVC in PHP. However, how should M, V, c be implemented in PHP? Is MVC really a good fit for PHP? Here easy to give some of their own thinking.
Recently, design patterns are popular, MVC is ubiquitous, and the PHP field is no exception, and many forums are beginning to discuss the use of MVC in PHP. However, how should M, V, c be implemented in PHP? Is MVC really a good fit for PHP? Here easy to give some of their own thinking.
MVC's prototype Earl's casino
MVC exists in the desktop program, m refers to the data model, v refers to the user interface, C is the controller. The purpose of using MVC is to separate the implementation code for M and v so that the same program can use a different representation. For example, a batch of statistical data can be represented by a histogram, pie chart, respectively. C exists to ensure the synchronization of M and V, and once M is changed, V should be updated synchronously.
MVC in Java
Java introduced MVC into the web domain, and on that basis, a set of systems called Model2 was architected. Because of the particularity of the web, MVC in Java is not exactly the same as in desktop. The main reason is that V in the Web is not continuous, the user every visit, V will be regenerated once, so V is always consistent with m, do not need C to control synchronization. So what's C in Java doing? C in Java is usually used for the steering of the process, and its utility is the dispatch mode, which is no longer the C in desktop.
MVC in PHP
It is not possible to copy Java's MVC in PHP intact. The problem is mainly on M, in Java, M is a data model that is independent of business logic and performance logic, and exists across pages in the server side, and Java beans play the same color. The PHP process, which does not reside in memory for a long time, is created only when the PHP page starts executing, and ends when the page interpretation is executed. In such cases, we simply cannot achieve m directly. So all PHP programs that claim to implement the MVC pattern can only implement m by means of simulations. The way to do this is to store the data in a 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. This is a much more expensive approach than a bean in Java, and a simple operation that writes data to memory now needs to be sent from the server to the client or database and back again. Instead of maintaining a data model with so much overhead, and then finally putting the model back into the database, it's better to update the data inside the database directly as needed.
Now the MVC schema in PHP can be used to represent.

And then we'll look at v. Now many PHP programs simply use some template technology to write "Adopt MVC Architecture" in the description. In fact, the vast majority of PHP template programs are just separate HTML and PHP, rather than MVC emphasizes the separation of the display logic and business logic. A page that mixes HTML with PHP can be part of MVC. There is no satisfactory solution to how to separate the display logic and business logic. As for C, as in Java, or as a diversion, this is not really important in PHP.

An extremely resource-intensive m + a small difference of V + dispensable C constitutes the so-called MVC in PHP.
Based on the above analysis, Easy believes that PHP's original 3-tier architecture is more efficient than the so-called MVC, while the 3-layer architecture of the presentation layer to solve the problem of MVC also exists in the V. An improved 3-tier architecture is more appropriate 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.