Table of Contents
A brief analysis of MVC programming ideas in PHP programming, mvc programming ideas
What are the important website programming ideas in PHP?
Helping php mvc mode
Home Backend Development PHP Tutorial A brief analysis of MVC programming ideas in PHP programming, mvc programming ideas_PHP tutorial

A brief analysis of MVC programming ideas in PHP programming, mvc programming ideas_PHP tutorial

Jul 13, 2016 am 10:22 AM
mvc php

A brief analysis of MVC programming ideas in PHP programming, mvc programming ideas

PHP’s MVC programming idea has been widely used in the development of various large-scale projects. Many mature MVC frameworks have gradually become known to everyone and are widely used in various projects. The more common ones are ThinkPHP, codeigniter, Symfony, yii, cakePHP, etc. This article will briefly describe the MVC programming ideas of PHP.

1. What is MVC

To put it simply, it means classifying and layering the website source code.
The meaning of the three letters of MVC:
M: Model model, responsible for database operations.
V: View, responsible for calling the Model to retrieve data, and then calling the template to show the final effect.
C: Controller, the entry point of the program, decides which View to call and tells the View what to do.
In this way, the execution order of the program is C-V-M or C-M, which is exactly the opposite of the name of MVC.

2. Why MVC

1.It can make the physical structure of the website program more reasonable.

When building a website with PHP, the stupidest way is to build each page into a PHP file. If your website only has three pages: index.php and menu.php.article.php, then you don’t need MVC. But when we make a general website, we often have dozens of pages. It is obviously not appropriate to put all the pages in the root directory. What we can accept, so you need a reasonable idea to classify your code, divide them into different directories according to functions, and intelligently load and call them by the program. This is what MVC will help you do.

2.Make the code easier to maintain.

Let’s look at a single page again. The stupidest way is to mix PHP code and HTML code. This is obviously not good enough. When maintaining the website, you have to distinguish where is PHP and where is HTML. This is very difficult for a programmer to do. Say, it's a disaster. So many people use Smarty, so that they can separate "data processing" and "page display". This is indeed good, and many people are doing it, but this is not MVC. What MVC has to do is to separate "data processing" ” is further divided into “logical processing” and “database operations”, which is what is called layering.
In this way, when your program has an error or you want to modify it, it becomes very easy. When the page displays an error, you check V or the template file; when there is a problem with the logic, you check C and V ;When your database operation error occurs, check M.
In fact, MVC generally divides a PHP page into 4 pages, namely C, V, M, and template. Each performs its own duties to facilitate management.

3.Conducive to code reuse.

MVC will generally put a large function in a directory, which is managed by a C.
For example, if we want to build a website with a membership system, we can put all the membership-related codes in the user directory and manage them uniformly by User_Controller. When another website of ours also needs a membership system, we can directly copy this directory. In the past, just modifying the interface was enough.

3. The idea of ​​implementing MVC in PHP

We need three base classes: Controller, View, and Model, and then different C, V, and M inherit them respectively to have corresponding properties and methods. If you don’t understand here, you can read an object-oriented book. .

Here is a design idea for the MVC base class, for reference only:

1. Design of Controller class

A main() method for the program to call, mainly deciding how to process it through get and post variables.
A getModel($model) method calls M in the corresponding directory when the database needs to be called.
A display($view) method, called in the main() method, loads the corresponding V, and replaces the main() method of the corresponding V;

2.The design of View class is very similar to Controller

A main() method, this method is called when C loads V so that the program can continue to execute.
A getModel($model) method calls M in the corresponding directory when the database needs to be called.
A display($template) calls the corresponding template file and passes the data to the template.

3.Design of Model class

You can define some attributes, such as which tables to operate, which fields to operate, etc.
A getDB() method to obtain an instance of a database class (database classes are generally designed using the singleton mode)
A load() method loads a data.
An add() method can automatically construct SQL statements based on defined attributes and perform insertion operations.
An eidt() method, same as above, but performs modification operations.
A del() method, same as above, but performs a delete operation.
In order to enable novices to better understand the working principle of my idea, we now simulate a user login scenario to see how MVC works.
Now assume that all data is submitted to index.php,

Step one:
We submit each get variable and tell index.php which C to use. For example, index.php can be like this? controller=user
Then index receives the get variable, and does not need to do anything. It directly finds /user/controller.php and throws all the data to it. Originally GET and POST are global, so index.php does not need to do anything and directly calls C. The main function is enough, and the task of index.php is completed.

Step 2:
The main function of C starts to execute, checks the variables, and finds the login operation that the user wants to perform (very simple, you just post the variable do=login), then calls getModel and loads the corresponding M class (for example, /user/models/ model.php), and instantiate it, call the load method of the instance, load the user's data, and determine whether it is consistent with the password submitted by the user. If the submitted data is incorrect, the header will jump to the error page. If it is correct, call display () method, load the corresponding V (such as /user/views/details.php), instantiate it, call its main() function, and enter the third step. At this point, the task of C has been completed, and the second operation is performed in the main function.

Step 3:
You can choose to call getModel() to load M and rewrite the retrieved data, or you can pass the parameters (such as SESSION) when C instantiates V. After V has determined to get the data, display() and load Template, MVC execution is completed.
Of course, due to word count and energy limitations, what is written here is only a very brief summary. There are many details to consider during actual implementation. But when I designed MVC, this was the general idea, and it has been used in practice, and it feels good.

What are the important website programming ideas in PHP?

First of all, you need to figure out what MVC is used for. The role of MVC is in cooperative development. For example, you have a development team. You are the director, and you need to use a structure to allow your team members to collaborate with each other and develop in parallel. And when you are fighting alone, there is really no need to use a framework like MVC. On the contrary, over-reliance on MVC ends up making metaphysical things, which is not efficient.
Social networking sites, mvc can’t solve much of your problem. Even if you don’t use object-oriented, all pages are stored in procedures, which will make it more efficient.
The same goes for object-oriented. When you need multiple instances, just create a class to facilitate operation. When you only need one object, object-oriented is meaningless.
Don’t think about writing frameworks at the beginning of development, such as mvc, use the most direct way to make the first version as quickly as possible, and then make improvements based on demand first. This is the most appropriate method. .

Helping php mvc mode

mvc is just a programming idea, regardless of language

Many projects in PHP development use mvc. This model is suitable for the development of large projects

MVC is three The abbreviations of the words are: Model, View and Controller). The purpose of the MVC model is to realize the functional division of labor in the Web system. The Model layer implements the business logic in the system, which can usually be implemented using JavaBean or EJB. The View layer is used for interaction with users, usually implemented with JSP. The Controller layer is the communication bridge between Model and View. It can dispatch user requests and select appropriate views for display. It can also interpret user input and map them into operations that can be performed by the Model layer

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/851341.htmlTechArticleA brief analysis of MVC programming ideas in PHP programming, mvc programming ideas PHP's MVC programming ideas have been widely used For the development of various large-scale projects, many mature MVC frameworks have gradually been adopted by everyone...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles