Home >Backend Development >PHP Tutorial >In-depth understanding: single entry, MVC, ORM, CURD, ActiveRecord concepts_PHP tutorial
MVC
MVC是一个设计模式,它强制性的使应用程序的输入、处理和输出分开。使用MVC应用程序被分成三个核心部件:模型(M)、视图(V)、控制器(C),它们各自处理自己的任务。
视图 :视图是用户看到并与之交互的界面。对老式的Web应用程序来说,视图就是由HTML元素组成的界面,在新式的Web应用程序中,HTML依旧在视图中扮演着重要的角色,但一些新的技术已层出不穷,它们包括Adobe Flash和象XHTML,XML/XSL,WML等一些标识语言和Web services。如何处理应用程序的界面变得越来越有挑战性。MVC一个大的好处是它能为你的应用程序处理很多不同的视图。在视图中其实没有真正的处理发生,不管这些数据是联机存储的还是一个雇员列表,作为视图来讲,它只是作为一种输出数据并允许用户操纵的方式。
模型 :模型表示企业数据和业务规则。在MVC的三个部件中,模型拥有最多的处理任务。例如它可能用象EJBs和ColdFusion Components这样的构件对象来处理数据库。被模型返回的数据是中立的,就是说模型与数据格式无关,这样一个模型能为多个视图提供数据。由于应用于模型的代码只需写一次就可以被多个视图重用,所以减少了代码的重复性。
控制器 :控制器接受用户的输入并调用模型和视图去完成用户的需求。所以当单击Web页面中的超链接和发送HTML表单时,控制器本身不输出任何东西和做任何处理。它只是接收请求并决定调用哪个模型构件去处理请求,然后确定用哪个视图来显示模型处理返回的数据。
现在我们总结MVC的处理过程,首先控制器接收用户的请求,并决定应该调用哪个模型来进行处理,然后模型用业务逻辑来处理用户的请求并返回数据,最后控制器用相应的视图格式化模型返回的数据,并通过表示层呈现给用户。
面向对象编程(Object Oriented Programming,OOP,面向对象程序设计)是一种计算机编程架构。OOP 的一条基本原则是计算机程序是由单个能够起到子程序作用的单元或对象组合而成。OOP 达到了软件工程的三个主要目标:重用性、灵活性和扩展性。为了实现整体运算,每个对象都能够接收信息、处理数据和向其它对象发送信息。OOP 主要有以下的概念和组件:
组件 - 数据和功能一起在运行着的计算机程序中形成的单元,组件在 OOP 计算机程序中是模块和结构化的基础。
抽象性 - 程序有能力忽略正在处理中信息的某些方面,即对信息主要方面关注的能力。
封装 - 也叫做信息封装:确保组件不会以不可预期的方式改变其它组件的内部状态;只有在那些提供了内部状态改变方法的组件中,才可以访问其内部状态。每类组件都提供了一个与其它组件联系的接口,并规定了其它组件进行调用的方法。
多态性 - 组件的引用和类集会涉及到其它许多不同类型的组件,而且引用组件所产生的结果得依据实际调用的类型。
继承性 - 允许在现存的组件基础上创建子类组件,这统一并增强了多态性和封装性。典型地来说就是用类来对组件进行分组,而且还可以定义新类为现存的类的扩展,这样就可以将类组织成树形或网状结构,这体现了动作的通用性。
由于抽象性、封装性、重用性以及便于使用等方面的原因,以组件为基础的编程在脚本语言中已经变得特别流行。
Object-Relational Mapping (Object/Relation Mapping, referred to as ORM) was developed with the development of object-oriented software development methods. The object-oriented development method is the mainstream development method in today's enterprise-level application development environment, and the relational database is the mainstream data storage system that permanently stores data in the enterprise-level application environment. Objects and relational data are two forms of expression of business entities. Business entities are represented as objects in memory and as relational data in the database. There are associations and inheritance relationships between objects in memory, but in the database, relational data cannot directly express many-to-many associations and inheritance relationships. Therefore, object-relational mapping (ORM) systems generally exist in the form of middleware, which mainly implements the mapping of program objects to relational database data.
Object-oriented is developed based on the basic principles of software engineering (such as coupling, aggregation, encapsulation), while relational database is developed from mathematical theory. There are significant differences between the two sets of theoriesDifference. In order to solve this mismatch phenomenon, object-relational mapping technology came into being.
CURD is an abbreviation in database technology. The basic functions of various parameters in general project development are CURD. It represents create (Create), update (Update), read (Read) and delete (Delete) operations. CURD defines basic atomic operations for processing data. The reason why CURD is elevated to the level of a technical difficulty is because completing an activity related to aggregation involving CURD operations in multiple database systems, the performance of which may vary depending on the data relationship. There is a very big difference due to changes.
CURDIn specific applications, the methods of create, update, read and delete are not necessarily used, but the functions they complete are the same. For example, ThinkPHP uses the add, save, select, and delete methods to represent the CURD operations of the model.
ActiveRecord also belongs to the ORM layer, which was first proposed by Rails and follows the standard ORM model: tables map to records, records map to objects, and fields map to object properties. With the following naming and configuration conventions, the operation of the model can be realized quickly to a large extent, and it is concise and easy to understand.
ActiveRecordThe main idea is:
1. Each database table corresponds to a class, and each object instance of the class corresponds to a row of records in the table in the database; Usually each field of the table has a corresponding Field in the class;
2. ActiveRecord is also responsible for persisting itself, and encapsulates the database in ActiveRecord Access, namely CURD;;
3. ActiveRecord is a domain model (Domain Model) that encapsulates part of the business logic;
ActiveRecord is more suitable for:
1. The business logic is relatively simple. When your class basically corresponds to the table in the database, ActiveRecord is very convenient , that is, most of your business logic operates on a single table;
2. When cross-table operations occur, transaction scripts (Transaction Script) are often used to upgrade cross-table transactions to transaction scripts Medium;
3. ActiveRecordThe biggest advantage is simplicity and intuitiveness. One class includes data access and business logic. It is more convenient if used with a code generator;
These advantages make ActiveRecord particularly suitable for rapid WEB development.
Single entry usually means that a project or application has a unified (but not necessarily unique) entry file, which means that all functional operations of the project are performed through this entry file, and often the entry file is the first One step is executed.
The advantage of a single entrance is that the overall project is more standardized, because the same entrance often has the same rules for different operations. Another aspect is that the benefit of a single entrance is that the control is more flexible, because interception is convenient, and judgments and operations such as some permission controls and user logins can be processed uniformly.
Some people may worry about whether all websites are accessed through one entry file, which will cause too much pressure. In fact, this is an unfounded idea.