Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to correctly understand the MVC model developed by PHP

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, Xiaobian will bring you about how to correctly understand the PHP development MVC model. The article is rich in content and analyzes and narrates from a professional perspective. After reading this article, I hope you can gain something.

PHP development MVC model for beginners still do not understand the meaning of it.

Using MVC surprised me by adding so many database operations to the program that performance dropped. MVC is just a framework and has nothing to do with database operations. MVC only provides a clear programming development pattern, as long as you handle it well, it is impossible to do many unnecessary database operations. An MVC architecture is not a good MVC architecture if it allows a programmer to perform many database operations without knowing it. I think MVC only needs to provide a simple development framework, there is no need to integrate a lot of library classes, library classes *** can let programmers choose to use.

I haven't studied the theory of MVC in depth, for me personally, the model is a database encapsulation, call the model method, you can get the corresponding data, but the details of the implementation programmers do not need to care. 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. By selecting (), you can query and update. The model should also be independent of the specific database type, regardless of whether you are using MySQL,Oracle or SQL Server.

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

The following is a rough result of PHP developing the MVC model. Not the full code, see demo package for full code.

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

In this model, I use arrays and database fields to map. Early PHPBEAN uses objects to correspond. But then I felt that this PHP development MVC model method was not good in PHP, and added a lot of unnecessary classes. Using arrays is more convenient and works better (arrays in PHP are really a good thing, much better than JAVA).

In the demo below, I use the mysql database to demonstrate, where the database operation class is changed from an original library class.

Below, explain in detail how to use demo.^_^

Add index.php to the original package

< ? require_once(SITE_PATH.'/ libs/phpbean.class.php'); require_once(SITE_PATH.'/ libs/mysql.class.php'); $phpbean=new phpbean(); global $phpbean; $mysql=new mysql("localhost" ,"****","****","52site"); $phpbean->

register('db',$mysql);

unset($mysql);

?>

This section of PHP development MVC model code is mainly MYSQL register to register inside, register on the use of the principle, you can see my translation of the two articles.

Then create a new mysqlController.class.php file with the following code:

< ? /** * MVC演示demo * 仅仅实现最基本的MVC功能,不包含安全处理 ,数据过滤,及其他优化措施。 * @author:feifengxlq * @since:2007-1-24 * @copyright http://www.phpobject.net/blog/ */ class mysqlController { var $module; function mysqlController(){ require_once(SITE_PATH.'/libs/module.class.php'); $this->

module=new module('52site_siteinfo');

//52site_siteinfo is the table name

$this->module->query("set names 'gb2312'");

//If it is MYSQL5, please add this sentence

}

function indexAction(){

print_r($this->module->select());//This enables reading data

}

}

?>

The first is to add a model to the constructor of the controller. Then call the model's methods in indexAction to display the data. This makes for the simplest query list.

Later I will write a concrete demo showing how to use PHP to develop other methods of the MVC model, such as queries, updates, additions, paginated lists, multi-table lookups, etc.

The above is how to correctly understand PHP development MVC model shared by Xiaobian for everyone. If there is a similar doubt, please refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report