Procedural programming vs. object-oriented programming

Source: Internet
Author: User
Tags drupal
Introduction: The difference between procedural programming and object-oriented programming is not whether to use functions or classes. that is to say, procedural programming may be used for classes or objects, only functions are used, and object-oriented programming is also possible. So where are their differences? Procedural programming Wikipedia... "> <LINKhref =" http://www.php100.com//statics/style/headfl

Introduction: The difference between procedural programming and object-oriented programming is not whether to use functions or classes. that is to say, procedural programming may be used for classes or objects, only functions are used, and object-oriented programming is also possible. So where are their differences?

Procedural programming

Wikipedia defines Procedural Programming in this way (Procedural Programming): Procedural Programming is equivalent to imperative Programming (executing a specified step to reach a predetermined state, it is also a programming example (as described in this article) that is derived from structured programming and follows the concept of process calling.

This is an appropriate definition, but we can also improve it. I agree with the "procedural programming is just a series of commands for specific steps to implement the required functions. How it is implemented is only the details and has nothing to do with examples. what is important is that it is necessary for work. Let's look at several examples:

This is obviously procedural programming:

 

$ M = mysqli_connect (...);

$ Res = mysqli_query ($ m, $ query );

$ Results = array ();

While ($ row = mysqli_fetch_assoc ($ res )){

$ Results [] = $ row;

}

 

Although objects are used, they are actually procedural programming:

 

$ M = new MySQLi (...);

$ Res = $ m-> query ($ query );

$ Results = array ();

While ($ row = $ m-> fetch_assoc ($ res )){

$ Results [] = $ row;

}

 

Even if a class is used, it is still procedural programming:

 

Class GetResults {

Public function getResults (){

$ M = new MySQLi (...);

$ Res = $ m-> query ($ query );

$ Results = array ();

While ($ row = $ m-> fetch_assoc ($ res )){

$ Results [] = $ row;

}

Return $ results;

}

}

 

Note: The above examples use identical code boxes. The difference between them is how to implement them, but all of them are procedural programming and all contain necessary independent steps. Let's take a look at what is object-oriented programming and what is the difference between them?

 

Object-oriented programming

On Wikipedia, Object-Oriented Programming is defined as follows: object-oriented programming uses the object programming paradigm-including data domains, methods, and interactions between them-to design applications and programs. Programming techniques include data abstraction, encapsulation, communication, modularity, polymorphism, and inheritance.

This definition is good, but I only agree with the second part. In the first part, it is obvious that "object must be used for object-oriented programming" is incorrect, you can use data abstraction, encapsulation, communication, modularity, polymorphism, and inheritance to abstract data.

My understanding of object-oriented programming is as follows:

First, it must abstract the data into a module structure;

Second, there must be some way to implement code multi-state execution;

Finally, it can at least partially compress code and functions.

Let's take a look at several examples:

Classic object-oriented programming mode:

 

Class Mediator {

Protected $ events = array ();

Public function attach ($ eventName, $ callback ){

If (! Isset ($ this-> events [$ eventName]) {

$ This-> events [$ eventName] = array ();

}

$ This-> events [$ eventName] [] = $ callback;

}

Public function trigger ($ eventName, $ data = null ){

Foreach ($ this-> events [$ eventName] as $ callback ){

$ Callback ($ eventName, $ data );

}

}

}

$ Mediator = new Mediator;

$ Mediator-> attach ('load', function () {echo "Loading ";});

$ Mediator-> attach ('stop', function () {echo "Stopping ";});

$ Mediator-> attach ('stop', function () {echo "Stopped ";});

$ Mediator-> trigger ('load'); // prints "Loading"

$ Mediator-> trigger ('stop'); // prints "StoppingStopped"

 

The same mode, but the function is used:

 

$ Hooks = array ();

Function hook_register ($ eventName, $ callback ){

If (! Isset ($ GLOBALS ['hooks'] [$ eventName]) {

$ GLOBALS ['hooks'] [$ eventName] = array ();

}

$ GLOBALS ['hooks'] [$ eventName] [] = $ callback;

}

Function hook_trigger ($ eventName, $ data = null ){

Foreach ($ GLOBALS ['hooks'] [$ eventName] as $ callback ){

$ Callback ($ eventName, $ data );

}

}

 

As you can see, they all follow the transmitter Pattern and are designed to decouple caller from the sender, so they are all object-oriented. Both provide status and are modular. The difference is that the first one is implemented through the traditional class (so reusable, which is also an advantage of using the class), and the second one uses global variables and is not reusable. Here I use "hook", which is an event system used by Drupal.

Drupal is a good example. its module system, "hook" system, and structure system are all object-oriented, but they are not implemented using objects. they use functions and dynamic allocation, this leads to a lot of embarrassing trade-offs. I am not saying that this is a good object-oriented program, but it just proves that classes are not necessary for object-oriented programming.

Why is this important?

It is very simple, because many developers think that they are doing line-oriented programming when using classes; others think that they are doing programming by using functions, which is not correct. Procedural programming and object-oriented programming are both a way to write code, rather than a means to write code. Will you follow the steps to write the program in the set way? You seem to be in functional programming, but if you focus on state change and sealed abstraction, you are using object-oriented programming.

Class is just a tool that helps simplify object-oriented programming. it is not a requirement or indicator of object-oriented programming.

Object-oriented programming and database access

What is database access in object-oriented programming? Object-oriented programming database access is completely abstract. my method is:

 

$ Mapper = new PersonDataMapper (new MySQLi (...));

$ People = $ mapper-> getAll ();

 

People is an array of person objects. Note: it is necessary to abstract things like this, so things objects cannot directly operate on the database. you need a er to translate the conversion between things objects and data storage. A dedicated ER creates an internal request, executes the request, and returns the result. However, this is completely abstract. we can simply replace the er to change the implementation details of the database layer.

The responsibility for data persistence becomes encapsulation abstraction, which is why it is object-oriented programming rather than procedural programming.

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.