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.
The prototype of MVC
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.
http://www.bkjia.com/PHPjc/446700.html www.bkjia.com true http://www.bkjia.com/PHPjc/446700.html techarticle 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? MVC really suits ...