The difference between object-oriented and process oriented php

 Watson Blog October 2, 2017 23:51:41 PHP technology comment nine hundred and thirty-seven Reading mode
abstract

Php programming is divided into process oriented and object-oriented. There is no difference in function implementation between the two, but there is a big difference in code writing. The process oriented code is scattered and difficult to manage and maintain, while object-oriented encapsulates common functions into a class, which is easy to maintain and manage.

object-oriented , is to encapsulate some commonly used operations into classes for easy calling. You can call them where you need them, which is convenient for development and maintenance! Modify this encapsulated class to achieve the goal of modifying the whole site!

The three characteristics of object-oriented (encapsulation, inheritance, polymorphism) ensure the efficiency and correctness when doing complex things.

Process oriented That is to encapsulate the code into sub processes or functions, and use separate code to operate in each place. If the development code is repeated, it is cumbersome, and maintenance is relatively troublesome. Where you modify it, it only works.

php面向对象和面向过程的区别

The difference between object-oriented and process oriented php

In the actual development process, both "object-oriented" and "process oriented" ideas can be used. At present, the most popular idea is to take object-oriented as the main body and process oriented as the auxiliary.

Here is a small example:

Process oriented: For beginners of PHP, the most basic connection database and query database will be written as follows:

  1. <? php
  2. ? $Con ?=? mysql_connect(.........);
  3. ? mysql_query('set? names?utf8');
  4. ? mysql_select_db(....);
  5. ? $query ?=? mysql_query(? $sql ?);
  6. ? while (? $Rs ?=? mysql_fetch_aray(? $query ?)?)? {
  7. ?? echo ? $Rs [0];
  8. ?}

As above, if you operate the database for 10 times, you can write such code for 10 times!

Object oriented: create a php file and encapsulate the above code into a class:

  1. <? php
  2. ? class ? mysql{
  3. ??? var ? $Con ;
  4. ??? var ? $table ;
  5. ??? public ? ConnEct(? $local ,? $root ,? $pass ,? $base ,? $code ){
  6. ???? $this ?->? Con?=?mysql_connect(? $local ,? $root ,? $pass );
  7. ???? mysql_query('set? names?'?.? $code );
  8. ???? mysql_select_db(? $base ?);
  9. ???}
  10. ?? public ? Tab(? $Table )? {
  11. ??? $this ?->? table?=? $Table ;
  12. ??? return ? $this ;
  13. ??}
  14. ?? public ? Select(){
  15. ???? $rs ?=? mysql_query('select?*? from?'?.? $this ?->? table)
  16. ???? while (? $Rs ?=? mysql_fetch_array(? $rs ?)? {
  17. ?????? $Rule []?=? $Rs ;
  18. ????}
  19. ?? return ? $Rule ;
  20. ??}
  21. ?}

Save the above code as a file, such as mysql.php, and import this file where you need to operate the database. It is very convenient to query a table of the database!

  1. <? php
  2. ?? include_once ?' mysql.php';
  3. ?? $Mysql ?=? new ? mysql;? //Instantiate a class;
  4. ?? $Mysql ?->? ConnEct('localhost',?' root',?123456,?'table',?'utf8'); //Connect Database
  5. ?? $Resul ?=? $Mysql ?->?? Tab('user')?->? Select(); //Query the user table and return array results
  6. ?? print_r(? $Resul ?);? //Print this array

The Dispute of PHP Programming Style

Object oriented and process oriented programming languages can only use one of the two in many programming languages, but PHP is different from other programming languages, that is, we can freely choose or mix PHP object oriented and PHP process oriented programming languages. This often causes developers to discuss which development style to choose.

PHP process oriented

The advantage of process orientation is that it runs fast. Two popular process oriented PHP programs are OsCommerce and PhpMyAdmin. The process oriented coding method is mainly used. They are fast to build and run. Both naturally adopt the method of embedding HTML.

The above two programs using the process oriented style have very good documentation and code comments. The development framework provided by OsCommerce can increase maintainability and extensibility. However, neither of them provides APIs and cannot extend programs to other systems.

If you want to integrate OsCommerce into a billing program, you need to spend a lot of time and effort, just like expanding PhpMyAdmin into a back-end management tool for customers. However, from the point of view of their design purpose, they really perform well in their respective fields.

PHP Object Oriented

The advantage of object-oriented is extensibility and encapsulation. Just writing code in an object-oriented way will not generate documents for your code, but it can encourage you to add documents to it. In addition, you may write an API for easy extension.

Two popular object-oriented php programs are Smarty and FPDF. It mainly uses the object-oriented coding method. Smarty and FPDF both provide well documented APIs to extend the main class. This shows the necessity of organizing methods and data within a class -- sometimes the same function can be completed with functions and global variables, but it is not easy to expand. Moreover, using objects is very helpful for tracking and maintaining the style of PDF or HTML documents. You can publish the same data in different formats. Smarty and FPDF are excellent examples of using objects to build flexible and practical class libraries.

The two programming methods have their own advantages

  • The practicability and scalability of Smarty and FPDF
  • The running speed and good performance of osCommerce and phpMyAdmin

This option also includes some basic development of PHP. PECL and PEAR have received a lot of praise and criticism. I think these two projects provide good examples to illustrate the difference between PHP process oriented and object-oriented programming.

PECl provides PHP extension libraries, which are developed in C and process oriented ways, and focus on speed and simplicity. Usually, these are ported from existing LGPL software, and many interesting features have been added to PHP. After all, PHP is written in C.

PEAR has contributed many interesting classes, such as creating Excel tables or changing DNS records. Using the PEAR class library can save you a lot of time, and even allow you to develop when you are not familiar with PHP - "I don't understand but it works!".

Which method should we choose when writing programs?

At the beginning of a project, the actual coding purpose and direction should be sought first. What is the goal of this project? Here is the possible answer.

  1. Rapid development and release (development efficiency)
  2. Run as fast as possible (operational efficiency)
  3. Easy to maintain, improve and expand (maintenance efficiency)
  4. Publish an API

The first and second directions tend to use the procedural style, while the last two directions tend to use the PHP object-oriented style.

Netizens' opinions:

Actually? Small, personal, without secondary or iterative development, it can be process oriented, and the coding idea is simple and convenient; However, for multiple cooperative, medium-sized and above, and new functions need to be added later, it is strongly recommended to use object-oriented, code encapsulation, abstraction, inheritance, and polymorphism...

In fact, all user-defined functions can also meet the project requirements. Object oriented is only used to make the program thinking clearer, which will be convenient when multiple people work together to develop.

For example, create a class User {} for the user (registration, audit, data, etc.)
It integrates various functions for user operation. Your colleagues or co development partners can directly use this User object to apply to other functions, such as calling a user's data $User ->get_profile ($uid); In this way, the idea is very clear. Otherwise, he needs to find your function library, and it is possible to waste a lot of time by finding one function after another.

If it is found that there is a process of reuse more than 2 times, it will try to reconstruct the object. To enhance scalability, keep low coupling.

For complex processes that are not repeated only once, write them quickly.

Process oriented is to analyze the steps needed to solve the problem, and then use functions to implement these steps step by step. When using these steps, you can call them one by one.

Object oriented is to decompose a problem transaction into various objects. The purpose of creating objects is not to complete a step, but to describe the behavior of a thing in the whole problem solving step.

For example, in Gobang, the process oriented design idea is the first step to analyze the problem: 1. start the game, 2. sunspots go first, 3. draw the picture, 4. judge the win or lose, 5. it's the turn of the white, 6. draw the picture, 7. judge the win or lose, 8. return to step 2, 9. output the final result. The problem is solved by implementing each of the above steps with a separate function.

The object-oriented design is to solve the problem from another idea. The whole Gobang can be divided into 1. Black and white sides, whose behaviors are identical. 2. The chessboard system is responsible for drawing the picture. 3. The rule system is responsible for judging violations, wins and losses. The first type of object (player object) is responsible for receiving user input and informing the second type of object (chessboard object) of changes in the layout of chess pieces. The chessboard object is responsible for displaying such changes on the screen after receiving the changes in the i of chess pieces. At the same time, the third type of object (rule system) is used to judge the chess game.

It is obvious that object-oriented is to divide problems by function, not by steps. It is also the drawing of chess games. Such behavior is scattered in the total multi-step process oriented design, and different drawing versions are likely to appear, because usually designers will consider the actual situation and make various simplifications. In object-oriented design, drawings can only appear in chessboard objects, thus ensuring the unity of drawings.

The unification of functions ensures the extensibility of object-oriented design. For example, I want to add the function of repentance. If I want to change the process oriented design, I will change a series of steps from input to judgment to display, and even adjust the sequence of steps on a large scale. If it is object-oriented, it is only necessary to change the chessboard object. The chessboard system saves the chess scores of black and white sides, which can be simply traced back, while the display and rule judgment are not considered. At the same time, the whole order of calling object functions is unchanged, and the changes are only local.

Another example is that I want to change the Gobang game to Go. If you are process oriented, the rules of Gobang are distributed in every corner of your program. Rewriting is better than changing it. But if you were originally an object-oriented design, you could only change the rule objects. Isn't the difference between Gobang and Go just rules? (Of course, the size of the chessboard seems different, but do you think this is a problem? Just make a small change in the chessboard object.) The general steps of playing chess have no change from the object-oriented perspective.

Of course, in order to make changes only locally, people who need to design have enough experience. Using objects cannot guarantee that your program is object-oriented. Beginners or very poor programmers are likely to practice process oriented with the illusion of object-oriented. The so-called object-oriented program designed in this way is difficult to have good portability and extensibility.

 Watson Blog
  • This article is written by Published on October 2, 2017 23:51:41
  • This article is collected and sorted by the website of Mutual Benefit, and the email address for problem feedback is: wosnnet@foxmail.com , please keep the link of this article for reprinting: https://wosn.net/644.html

Comment