Home > Backend Development > PHP Tutorial > Correctly understand the MVC model developed with PHP_PHP tutorial

Correctly understand the MVC model developed with PHP_PHP tutorial

WBOY
Release: 2016-07-15 13:33:02
Original
936 people have browsed it

Using MVC adds a lot of database operations to the program, which reduces performance, which really surprised me. MVC is just a framework and has nothing to do with database operations. MVC only provides a clear programming development model. As long as you handle it well, it is impossible to do a lot of unnecessary database operations. If an MVC allows a programmer to perform many more database operations without knowing it, it is definitely not a good MVC architecture. I think MVC only needs to provide a simple development framework. There is no need to integrate many library classes. It is best for programmers to choose to use library classes.

I have not studied the theory of MVC in depth. For me personally, the model is a database encapsulation. By calling the model method, you can get the corresponding data, but the programmer does not need to care about the implementation details. In actual development, it is likely that a database table corresponds to a model.

For example, a user information table userinfo corresponds to a model user. By calling the add() method of the model user, you can add a piece of data to the database. Through select(), you can query. update can be updated. At the same time, the model should be independent of the specific database type, whether you use mysql, oracle or sql server.

At the same time, I do not recommend using ROR in WEB development. It is so convenient and fast to use SQL language for complex multi-table queries, and the performance is better. If a programmer doesn't even have knowledge of SQL, I don't think he is a qualified programmer. Therefore, I provide a query method in my model to implement direct SQL query.

The following is an approximate result of developing the MVC model in PHP. This is not the complete code. Please see the demo package for the complete code.

<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?   </span></span></li><li><span>class module{   </span></li><li class="alt"><span>var $mysql;//数据库操作类,<br />可以是mysql,oracle,sql等等   </span></li><li><span>var $tbname;//模型对应的表名称   </span></li><li class="alt"><span>var $</span><span class="attribute">debug</span><span>=</span><span class="attribute-value">false</span><span>;//是否是调试模式   </span></li><li><span>function module($tbname,$</span><span class="attribute">db</span><span>=</span><span class="attribute-value">''</span><span>){}<br />//构造函数   </span></li><li class="alt"><span>function _setDebug($</span><span class="attribute">debug</span><span>=</span><span class="attribute-value">true</span><span>){}</span></li><li class="alt"><span>//开启或者关闭调试模式   </span></li><li><span>function add($row,$</span><span class="attribute">tbname</span><span>=</span><span class="attribute-value">''</span><span>){}</span></li><li><span>//新增加一条记录   </span></li><li class="alt"><span>function query($strsql){}//直接查询sql语句   </span></li><li><span>function count($</span><span class="attribute">where</span><span>=</span><span class="attribute-value">''</span><span>,$</span><span class="attribute">tbname</span><span>=</span><span class="attribute-value">''</span><span>){ }</span></li><li><span>//计数统计   </span></li><li class="alt"><span>function select($</span><span class="attribute">where</span><span>=</span><span class="attribute-value">''</span><span>,$</span><span class="attribute">tbname</span><span>=</span><span class="attribute-value">''</span><span>){}</span></li><li class="alt"><span>//查询   </span></li><li><span>function delete($</span><span class="attribute">where</span><span>=</span><span class="attribute-value">''</span><span>,$</span><span class="attribute">tbname</span><span>=</span><span class="attribute-value">''</span><span>){}</span></li><li><span>//删除满足条件的一个记录   </span></li><li class="alt"><span>function update($set,$where,$</span><span class="attribute">tbname</span><span>=</span><span class="attribute-value">''</span><span>){}</span></li><li class="alt"><span>//更新指定记录   </span></li><li><span>function detail($where,$</span><span class="attribute">tbname</span><span>=</span><span class="attribute-value">''</span><span>){}</span></li><li><span>//详细显示一条记录   </span></li><li class="alt"><span>}   </span></li><li><span class="tag">?></span><span>  </span></span></li></ol>
Copy after login

In this model, I use arrays and database fields to correspond. In the early PHPBEAN, objects were used to correspond. But later I felt that this method of developing MVC models in PHP was not good in PHP and added a lot of unnecessary classes. It is more convenient and better to use arrays (arrays in PHP are indeed a good thing, much better than JAVA).

In the demo below, I used the mysql database for demonstration, and the database operation class was changed from one of my original library classes.

Below, the use of demo will be explained in detail. ^_^
Add

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> ?   </span></span></li><li><span>require_once(SITE_PATH.'/<br />libs/phpbean.class.php');   </span></li><li class="alt"><span>require_once(SITE_PATH.'/<br />libs/mysql.class.php');   </span></li><li><span>$</span><span class="attribute">phpbean</span><span>=</span><span class="attribute-value">new</span><span> phpbean();   </span></li><li class="alt"><span>global $phpbean;   </span></li><li><span>$</span><span class="attribute">mysql</span><span>=</span><span class="attribute-value">new</span><span> mysql("localhost"<br />,"****","****","52site");   </span></li><li class="alt"><span>$phpbean-</span><span class="tag">></span><span>register('db',$mysql);   </span></span></li>
<li><span>unset($mysql);   </span></li>
<li class="alt">
<span class="tag">?></span><span>  </span>
</li>
</ol>
Copy after login

to the index.php of the original package. This PHP development MVC model code is mainly to register MYSQL into the register. About For the principles of using the registrar, you can read the two articles I translated.
Then create a new mysqlController.class.php file with the following code:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> ?   </span></span></li><li><span>/**   </span></li><li class="alt"><span>* MVC演示demo   </span></li><li><span>* 仅仅实现最基本的MVC功能,不包含安全处理<br />,数据过滤,及其他优化措施。   </span></li><li class="alt"><span>* @author:feifengxlq   </span></li><li><span>* @since:2007-1-24   </span></li><li class="alt"><span>* @copyright http://www.phpobject.net/blog/   </span></li><li><span>*/   </span></li><li class="alt"><span>class mysqlController   </span></li><li><span>{   </span></li><li class="alt"><span>var $module;   </span></li><li><span>function mysqlController(){   </span></li><li class="alt"><span>require_once(SITE_PATH.'/libs/module.class.php');   </span></li><li><span>$this-</span><span class="tag">></span><span class="attribute">module</span><span>=</span><span class="attribute-value">new</span><span> module('52site_siteinfo');<br>//52site_siteinfo为表名称   </span></span></li>
<li class="alt">
<span>$this-</span><span class="tag">></span><span>module-</span><span class="tag">></span><span>query("set names 'gb2312'");<br>//如果是MYSQL5请加上这句   </span>
</li>
<li><span>}   </span></li>
<li class="alt"><span>function indexAction(){   </span></li>
<li>
<span>print_r($this-</span><span class="tag">></span><span>module-</span><span class="tag">></span><span>select());//这样实现了读取数据   </span>
</li>
<li class="alt"><span>}   </span></li>
<li><span>}   </span></li>
<li class="alt">
<span class="tag">?></span><span>  </span>
</li>
</ol>
Copy after login

The above is first to add a model to the constructor of the controller. Then call the model method in indexAction to display the data. This implements the simplest query list.

In the future, I will write a specific demo to explain how to use PHP to develop other methods of MVC model, such as query, update, add, paging list, multi-table query, etc.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446086.htmlTechArticleUsing MVC adds a lot of database operations to the program, which reduces performance, which really surprised me. MVC is just a framework and has nothing to do with database operations. MVC just provides a...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template