Here is a small example:
-
<? php -
? $Con ?=? mysql_connect(.........); -
? mysql_query('set? names?utf8'); -
? mysql_select_db(....); -
? $query ?=? mysql_query(? $sql ?); -
? while (? $Rs ?=? mysql_fetch_aray(? $query ?)?)? { -
?? echo ? $Rs [0]; -
?}
-
<? php -
? class ? mysql{ -
??? var ? $Con ; -
??? var ? $table ; -
??? public ? ConnEct(? $local ,? $root ,? $pass ,? $base ,? $code ){ -
???? $this ?->? Con?=?mysql_connect(? $local ,? $root ,? $pass ); -
???? mysql_query('set? names?'?.? $code ); -
???? mysql_select_db(? $base ?); -
???} -
?? public ? Tab(? $Table )? { -
??? $this ?->? table?=? $Table ; -
??? return ? $this ; -
??} -
?? public ? Select(){ -
???? $rs ?=? mysql_query('select?*? from?'?.? $this ?->? table) -
???? while (? $Rs ?=? mysql_fetch_array(? $rs ?)? { -
?????? $Rule []?=? $Rs ; -
????} -
?? return ? $Rule ; -
??} -
?}
-
<? php -
?? include_once ?' mysql.php'; -
?? $Mysql ?=? new ? mysql;? //Instantiate a class; -
?? $Mysql ?->? ConnEct('localhost',?' root',?123456,?'table',?'utf8'); //Connect Database -
?? $Resul ?=? $Mysql ?->?? Tab('user')?->? Select(); //Query the user table and return array results -
?? print_r(? $Resul ?);? //Print this array
The Dispute of PHP Programming Style
PHP process oriented
PHP Object Oriented
-
The practicability and scalability of Smarty and FPDF -
The running speed and good performance of osCommerce and phpMyAdmin
Which method should we choose when writing programs?
-
Rapid development and release (development efficiency) -
Run as fast as possible (operational efficiency) -
Easy to maintain, improve and expand (maintenance efficiency) -
Publish an API
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.