4

Possible Duplicate:
OOP vs Functional Programming vs Procedural

Lately I've been introduced to the OO approach, so far i've been writing PHP using functions.

Now, honestly, I don't really get it: When I use the 'Function' idea, I just include a file named, for example, functions.php that has all the functions that I need including the vars, and when I need to use that piece of code, I just call it and set the vars, or leave it empty if there is default vars.

now as far as I understand OO, Instead of writing a lot of functions with no 'category', I just gather them around in a 'class' (for example all the functions for working with a db would be under 'db' class) - and instead of giving them vars, I declare those vars upfront in the class.

so, it feels like I'm basically doing the same thing. I know that the vars are not global in OOP, and they are per instance, which is OK for making more understand-able code, but besides that, I can't really feel a big difference in doing:

$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 it might be more readable for someone who didn't write that code, but can't see the big benefits everybody talking about: reuse, organized, faster and so on.

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

Thanks in advance, Eek.

Community
  • 1
  • 1
eek
  • 105
  • 1
  • 10
  • 3
    You don't mean "functional", you mean "procedural". Functional is something rather different (think Lisp). – Kerrek SB Dec 24 '11 at 14:35
  • PHP's "object oriented" support is largely tacked on as an afterthought if you ask me. If you find it useful, use it, but don't be surprised when some aspect of the language feels artifical, not thought out, or plain baffling. – Kerrek SB Dec 24 '11 at 14:36
  • 2
    If you're simply using classes as a namespace to group functions, then it isn't really OOP. – JohnP Dec 24 '11 at 14:36
  • Immutable variables are probably the biggest difference between "classic" functional and OO. – Svend Dec 24 '11 at 14:39

3 Answers3

2

read up on polymorphism. once you understand this you are halfway there. A class is more than simply an aggregation of functions (methods in oo speak), an instance of a class encapsulates state and behaviour. you should also study design patterns in order to fully realise the power of the oo paradigm. start with the gang of four (http://en.m.wikipedia.org/wiki/Design_Patterns) and also fowlers enterprise patterns (http://martinfowler.com/articles/enterprisePatterns.html)

Myles McDonnell
  • 12,943
  • 17
  • 66
  • 116
0

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

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

Alex
  • 32,506
  • 16
  • 106
  • 171
0

When all you do is write simple functions that don't retain any context etc, procedural programming is a lot easier.

Once you want to use two functions with exactly the same arguments, it's a lot easier to use objects, since it will require less code.

The examples below assume that you have a website (SO) and want to create URLs for separate pages. You'll want to define the "base location" in one central location to avoid having to re-write all pages once the domain changes.

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

Doing that with functions:

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

As you can see, you may not even have bothered to use functions.

Then there's the pre-OO approach to this :

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

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

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105