Object-oriented programming versus procedural programming. What is the big difference?

Possible duplicate:
OOP versus functional programming and procedural

Recently, I got acquainted with the OO approach, so far I have written PHP using functions.

Now, to be honest, I really don't understand: When I use the idea of ​​“Function”, I just include a file with a name, for example, functions.php, which has all the functions that I need, including vars, and when I need to use this snippet of code, I just call it and set vars or leave it empty if there are default values ​​of vars.

Now, as far as I understand OO, instead of writing many functions without a “category”, I just collect them in a “class” (for example, all functions for working with db will be under the “db” 'class) - and instead, to give them vars, I declare these vars in advance in the class.

So it looks like I'm basically doing the same thing. I know that Vars are not global in OOP, and they are for each instance, which is suitable for creating more understandable code, but in addition, I can not feel the big difference in work:

$html = new html(); $html->src='http://stackoverflow.com'; $html->desc='Great place to learn & share knowledge'; $html->link(); 

-

 html_link('http://stackoverflow.com','Great place to learn & share knowledge'); 

I agree that this may be more readable for those who did not write this code, but cannot see the big advantages that everyone is talking about: reuse, organization, speedup, etc.

Share your thoughts and maybe I will understand how I can benefit from OOP :)

Thanks in advance, Ik.

+4
source share
3 answers

read about polymorphism. as soon as you understand it, you are halfway there. A class is more than just an aggregation of functions (methods in a reservation), an instance of a class encapsulates state and behavior. You should also study design patterns to fully realize the power of the Oo paradigm. start with a gang of four (http://en.m.wikipedia.org/wiki/Design_Patterns) as well as corporate fowlers templates (http://martinfowler.com/articles/enterprisePatterns.html)

+2
source

The main essence of OOP is that you have functionality (methods) and data (properties) in one place, class / object. When you only write functions and use variables as separated ones.

Note: Functional programming is not what you are doing now. There is a functional programming paradigm where structures, such as loops, do not exist, and everything is expressed in functions.

0
source

When all you do is write simple functions that do not preserve any context, etc., procedural programming is much simpler.

Once you want to use two functions with exactly the same arguments, it is much easier to use objects, as this will require less code.

The examples below assume that you have a website (SO) and want to create URLs for individual pages. You want to define a “base location” in one central location to avoid re-recording all pages after changing a domain.

 $url = new URL('http://stackoverflow.com'); echo $url -> link('home'); // http://stackoverflow.com/home echo $url -> link('review'); // http://stackoverflow.com/review 

Doing this with functions:

 echo linkURL('http://stackoverflow.com', 'home'); echo linkURL('http://stackoverflow.com', 'review'); 

As you can see, you could not even use functions.

Then there is a preliminary approach to this:

 $context = prepareURL('http://stackoverflow.com'); // returns <something> echo createURL($context, 'home'); echo createURL($context, 'review'); 

This approach uses functions that still have context. However, as you may have noticed, this is very close to OO.

0
source

Source: https://habr.com/ru/post/1387878/


All Articles