Home  >  Article  >  Backend Development  >  Object-oriented and procedure-oriented programming methods in PHP_PHP tutorial

Object-oriented and procedure-oriented programming methods in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:33:45824browse


 Introduction
 “Real genius has the ability to correctly evaluate uncertain, risky and contradictory information.--Churchill”
 
 When using many programming languages, You can usually only use either object-oriented or procedural programming. In php(as the current mainstream development language), you can freely choose or mix them. At present, the vast majority of PHP (as the current mainstream development language) programmers use a process-oriented approach, because parsing the WEB page itself is very "procedural" (from one tag to another tag). Embedding procedural processing code in HTML is a very straightforward and natural way, so PHP (as the current mainstream development language) programmers usually use this method.
 
 If you are new to PHP(as the current mainstream development language), writing code in a process-oriented style is probably your only choice. But if you often go to php(as the current mainstream development language) forums and news groups, you should see articles about "objects". You may also have seen tutorials on how to write object-oriented php(as the current mainstream development language) code. Or you may have downloaded some off-the-shelf class libraries and tried to instantiate objects and use class methods - although you may not really understand why these classes work, or why you need to use object-oriented methods to implement functions. .
 
Should we use the "object-oriented" style or the "process-oriented" style? Both sides have supporters. Comments like "the object is inefficient" or "the object is great" are also heard from time to time. This article does not attempt to easily determine which of the two methods has the absolute advantage, but rather to find out the advantages and disadvantages of each method.
 
 The following is a code example for procedural style:
 
 (as the current mainstream development language)
 print "Hello, world." ;
 ?>
 
 The following is an object-oriented style code example:
 
 (as the current mainstream development language)
class helloWorld {
Function myPrint() {
print "Hello, world.";
}
}
$myHelloWorld = new helloWorld();
$myHelloWorld->myPrint ();
 ?>
 
If you want to know some basic knowledge of "object-oriented", please use Google search. There are many wonderful articles on the Internet.
 
 Who writes code like this?
To understand why this topic has become the flashpoint for wars of words on the forum, let’s look at some of the more extreme examples from each camp. Let’s look at “process fanaticism” and “object fanaticism.”See if their ideas sound familiar.
 
 Process Fanatical
  Process fanaticism was once criticized by computer teachers in class because this method did not use a more abstract implementation. Supporting the process-oriented person's point of view "it can work!" does not improve the level and grade of their programming. After graduation they may find a job writing drivers, file systems or other low-level programming where their attention is focused on speed and code refinement.
 
An extreme example of "process fanaticism" is resistance to objects and abstraction. They are always thinking about how to make the program run faster, and they don't care whether others can read their code. They often view programming as a competition rather than a team activity. In addition to php(as the current mainstream development language), their favorite programming languages ​​​​are C and assembly. In the php(as the current mainstream development language) world, they may develop PECL modules and contribute efficient code.
 
 Object Fanatics
 Object fanatics are passionate about writing code in an object-oriented style at all times. They have not really considered whether using this method will affect the execution efficiency of the program. Sometimes it feels like they enjoy abstract design concepts more than realistic code. They are usually project managers or document writers.
 
Object enthusiasts point out that without abstract design methods we would still be programming with 0s and 1s. They like to use pseudocode to describe problems. An extreme example is the object fanatic who still uses objects even though he or she knows that efficiency is sometimes sacrificed. In addition to php(as the current mainstream development language), their favorite languages ​​​​are Java and Smalltalk. In the world of php(as the current mainstream development language), they may develop PEAR modules and contribute very well-documented and easy-to-maintain code.
 
 Don’t be extreme and sarcastic
Do you know why forums are always full of prejudice? Your experience and attitude towards new things may be the reason. As programmers, we need to always be aware of these biases and be open to learning new things.
 
 What is your coding tendency?
Consider what preferences or tendencies you have when writing PHP (as the current mainstream development language) code. Often these preferences are rather subtle. Sometimes you may have the same preferences in every project. I personally prefer "elegant", but I don't want to define what constitutes "elegant" code here, that should appear in another article. However, theorized preferences are not necessarily appropriate for actual projects—rather, they are often biases.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508601.htmlTechArticleIntroduction "Real genius has the ability to correctly evaluate uncertain, risky and contradictory information. -- Churchill” With many programming languages, you can usually only use object-oriented or oriented...
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