Object-oriented and process-oriented programming methods in PHP

Source: Internet
Author: User


  Introduction
"True genius is capable of correctly evaluating uncertain, risky, and conflicting information. -- Qiu Gill"
  
When using many programming languages, you can only use object-oriented or process-oriented programming methods. In php (as the mainstream development language), you can freely choose or mix. Currently, most php (as the mainstream development language) programmers use process-oriented methods, because parsing WEB pages is very "procedural" (from one tag to another ). It is natural to Process Code during the embedding process in HTML, so php (as the mainstream development language) programmers usually use this method.
  
If you are new to php (as the mainstream development language), writing code in a process-oriented style is probably your only choice. However, if you often use php (as the mainstream development language) forums and newsgroups, you should see articles about "objects. You may also have seen tutorials on How to Write object-oriented php code (as the mainstream development language. Or you may have downloaded some ready-made class libraries and tried to instantiate the objects and use the class methods-although you may not really understand why these classes work, or why do we need to use object-oriented methods to implement functions.
  
Should we use the "Object-Oriented" style or the "process-oriented" style? Both parties have their own supporters. Discussions like "objects are inefficient" or "objects are great" are also heard. This article does not try to determine which of the two methods has absolute advantages, but to find out the advantages and disadvantages of each method.
  
The following is a process-oriented code example:
  
<? Php (as the mainstream development language)
Print "Hello, world .";
?>
  
The following is an object-oriented code example:
  
<? Php (as the mainstream development language)
Class helloWorld {
Function myPrint (){
Print "Hello, world .";
}
}
$ MyHelloWorld = new helloWorld ();
$ MyHelloWorld-> myPrint ();
?>
  
If you want to know some basic "Object-Oriented" knowledge, please use Google search, there are many wonderful articles on the Internet.
  
  Who writes code like this?
To understand why this topic has become a fuse for the Forum, let's look at some extreme examples of each camp. Let's take a look at "process fanatics" and "Object fanatics ". Let's see if they seem familiar.
  
  Process craze
The process craze was criticized by computer teachers in class because it did not use a more abstract implementation method. Support the process-oriented Viewpoint "it can work !" It cannot improve the programming level and grade. After graduation, they may find a job, write drivers, file systems, or other underlying-oriented programming, with their focus on speed and code refining.
  
The extreme example of "process fanaticism" is to resist objects and abstract them. They are always thinking about how to make the program run faster, regardless of whether others can understand their code. They often regard programming as a competition rather than a team activity. Apart from php (as the mainstream development language), their favorite programming languages are C and assembly. In the php (as the mainstream development language) World, they may develop PECL modules and contribute efficient code.
  
  Fanatic object
Object enthusiasts are keen to use the object-oriented style to write code at any time. They have not really considered whether using this method will affect the execution efficiency of the program. Sometimes people feel that they prefer abstract design concepts rather than real code. They are usually project managers or document writers.
  
The object enthusiast points out that if there is no abstract design method, we are still programming with 0 and 1. They like to use pseudo-code to describe the problem. An extreme example is that object enthusiasts still use objects even if they know they sometimes sacrifice efficiency. Apart from php (as the mainstream development language), their favorite languages are Java and Smalltalk. In the php (as the mainstream development language) World, they may develop PEAR modules and contribute well-documented and easy-to-maintain code.
  
  Don't be extreme or ironic
Do you know why forums are always filled with various prejudices? Your experience and attitude towards new things may be the cause. As programmers, we often need to pay attention to these prejudices and learn new things with an open mind.
  
  Your coding preferences?
Consider your preferences or preferences when writing php code (as the current mainstream development language. These preferences are usually relatively obscure. Sometimes you may have the same preferences in each project. I personally tend to be "elegant", but I don't want to define how the code is "elegant" here, it should appear in another article. However, theoretical preferences are not necessarily suitable for actual projects-on the contrary, they are often biased.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.