Home  >  Article  >  Backend Development  >  What is mvc framework? How to develop mvc framework in php (with code)

What is mvc framework? How to develop mvc framework in php (with code)

不言
不言Original
2018-07-25 15:02:153134browse

The MVC framework developed with PHP may not be able to understand its meaning for those who are new to PHP, but it does not matter. In today's article, I will talk to you in detail about my understanding of the MVC framework and how to develop the MVC framework with PHP. method.

Using MVC adds a lot of database operations to the program, which degrades performance. This 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 programmers do 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, no matter 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 a rough result of developing the MVC framework in PHP.

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

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 is explained in detail. ^_^
Add

< ?   
require_once(SITE_PATH.&#39;/libs/phpbean.class.php&#39;);   
require_once(SITE_PATH.&#39;/libs/mysql.class.php&#39;);   
$phpbean=new phpbean();   
global $phpbean;   
$mysql=new mysql("localhost","****","****","52site");   
$phpbean->register(&#39;db&#39;,$mysql);   
unset($mysql);   
?>

to the index.php of the original package. This PHP development MVC model code mainly registers MYSQL into the registrar. Regarding the principle of using the registrar, you can Check out the two articles I translated.
Then create a new mysqlController.class.php file, the code is as follows:

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

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.

Related recommendations:

Build your own PHP MVC framework

Do you understand MVC in PHP?

The above is the detailed content of What is mvc framework? How to develop mvc framework in php (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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