is MVC really good for PHP?

Source: Internet
Author: User
Tags implement php template

Recent design patterns are popular, MVC is ubiquitous, PHP is no exception, and many forums are beginning to discuss using MVC in PHP. However, how exactly should M, V, c be implemented in PHP? is MVC really good for PHP? Here easy to give some of their own thinking.

the prototype of MVC

MVC is originally 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 of M and v so that the same program can use different representations. For example, a batch of statistical data can be represented by a histogram, a pie chart, respectively. The purpose of C exists is to ensure that M and V are synchronized, and that if m changes, V should synchronize updates.

MVC in Java

Java introduced MVC into the web domain, and on this basis a set of systems called Model2. Because of the specificity of the web, MVC in Java is not exactly the same as that 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 and M consistent, do not need C to control synchronization. So what's C in Java doing? C in Java is usually used in the process of steering, its practical dispatch mode, is no longer a desktop C.

MVC in PHP

It is not possible to have copy Java MVC in PHP intact. The problem is mainly in M, in Java, M is a data model that is independent of business logic and presentation logic, and the Java Bean plays the role of the server-side across pages. The PHP process does not reside in memory for a long time, but is created only when the PHP page is executed, and ends when the page interpretation is finished. In such a case, we simply cannot implement m directly. So all the PHP programs that claim to implement the MVC pattern can only be implemented by means of simulations of M. This is typically done by storing the data in a database or cookie/session before the end of the current page, and then rebuilding m through the database or cookie/session on the next page. In this way, compared with the Java bean can be said to be a huge overhead, the original to write data in memory of the simple operation is now transferred from the server to the client or database, and then transmitted back. Instead of maintaining a data model with so much overhead, and then putting the model back into the database at the end, it's better to update the data inside the database directly as needed.

Now the MVC architecture in PHP can be represented in the following diagram.

  

And then we'll look at v. Now many PHP programs simply use some template technology in the description to write "Using MVC architecture." In fact, the vast majority of PHP template programs are just separate HTML and PHP, rather than MVC emphasizes the display logic and business logic separation. A mix of HTML and PHP pages can be part of MVC. How to separate the display logic from the business logic has not yet been a satisfactory solution. Personally, I agree with Http://www.phpe.net/articles/384.shtml's implementation plan. As for C, similar to Java, or being used as a diversion, this is not really important in PHP.

A very resource-intensive m + a small difference of V + optional C constitutes the so-called MVC in PHP.

Based on the above analysis, easy to believe that the original PHP 3-tier architecture than the so-called MVC more efficient, and the 3-tier architecture in the performance layer to solve the problem of the V in MVC also exists. An improved 3-tier 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.