OOP VS FUNCTION

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

OOP VS FUNCTION

Post by Jaxolotl »

I'm planning to learn seriously OOP and after reading O'Reilly's "Programming PHP" book I get curious about what Programmers have to say about it.
Have no pre-conceps about it.
I'd relly like to hear (read ;) ) what do you put on the balance when choosing the scripting 'grammar'.
For exaple why you prefer OOP and when ...or why you prefer function style and when.
advantages <=> disadvantages
It would be really interesting to read what you have to say about it and I think it may be usefull for many people.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

We've had several debates over OOP versus functions in the past. Have you read them?
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post by Jaxolotl »

I read many thread under forums.devnetwork.net searching "oop procedural" (not all because ther are more than 800) some of them are a little flamy :(
the one I like most is viewtopic.php?t=46359&highlight=oop++procedural
and
viewtopic.php?t=56504&highlight=oop++procedural
viewtopic.php?t=42236&highlight=oop++procedural
but what I'm trying to generate is a thread where programmers write "why I prefer " and not "why you don't have to use"
Many of the thread are oriented to an specific problem but this is a question independent of the specific problem.



For example I didn't find anything about a comparison of memory usage.
So supposing that:
1 - the script is written in OOP and in a procedural way
2 - the quality of the scripting is excellent in both cases

I prefer using OOP because:

1 - Less memory usage
2 - is faster
3 - is more versatile
4 - easier to read for other programmers
5 - less coding
6 - need more resources but use them in a better and mor efficient way


OR



I prefer using hybrid (OOP mixed with procedural) because:

1 - ...
2 - ...
3 - ...


OR


When I need to BLABLA I prefer OOP but when I need to do BLABLABLa I use procedural because:
1 - ...
2 - ...
3 - ...


----------------------------------------------------------
In my case I use Procedural because:
1 - The typology of Clients who I work to use to change the project vary fast with new funtionalities or changing the behaviour of a simple instruction so i finde easier to create a basic library of funtions (math, db abstraction, login, etc) and a specific set of functions (paging, mapping, layout staff, error handling, etc).
I consider that for working in OOP the Project should be enough stable (the concept) at the beggining or it'll be a headache... but I might be wrong.
Anyway I never use large OOP coding before so I'm very slow writing it and far from being efficient.



So my question is "What programmers that knows both procedure choose and why?"
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Code efficiency really has no bearing on how I code. I mean I'll make sure I am not doing something stupid like loading a 20,000 record table into an array on every page load, but I won't try to evaluate if something is an object if it performs slower than a function.

From my experience, well written OO code is easier to troubleshoot, maintain and reuse plus its self documenting. When you do the math programmer hours are much more expensive than additional processors or RAM.

I guess the trick is to get a site up and running first with well written maintainable code. After its getting a million hits, then you can worry about efficiency.

So yeah, I like OOP better for everything. I typically do not use procedural anything.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

viewtopic.php?t=37995 and subsequently viewtopic.php?t=30746 answer the questions you've asked so far for the most part.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

procedural code is just so ugly and sloppy to me...

procedural way to do nothing...

Code: Select all

function doSomethingNeat($input)
{
    return explode('.', $input);
}
function neatOutput($input)
{
    if(!is_array($input))
    {
        trigger_error('You need to send me an array!', E_USER_ERROR);
    }
    return implode('!', $input);
}

$name = $_GET['name'];
$name = doSomethingNeat($input);
echo neatOutput($name);
Object-oriented way to do nothing

Code: Select all

class Neat
{

    protected $_input = array();

    public function __construct($input)
    {
        $this->_input = explode('.', $input);
    }
    public function output()
    {
        return implode('!', $this->_input);
    }
}

$name = $_GET['name'];
$neat = new Neat($name);
echo $neat->output();
It seems to be that OOP is just another organization layer on top of procedural code...
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

My advice to you Jaxolotl is not to ask people "do you prefer oop or procedual" because you will just get opinions, and strong ones at that. You won't really get anywhere. A procedural programmer will resent me and present a long argument if, for example, someone was to say "procedural programmers are only procedural because they have failed to get oop". Whether this is true or not is not something I wish to debate nor assert.

You should learn oop, and properly (read about refactoring and design patterns), before you make a decision. Although, take heed, all the highest paid programmers know how to program with objects.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

You need OOP and procedural. They both have their place.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Jaxolotl wrote:In my case I use Procedural because:
1 - The typology of Clients who I work to use to change the project vary fast with new funtionalities or changing the behaviour of a simple instruction so i finde easier to create a basic library of funtions (math, db abstraction, login, etc) and a specific set of functions (paging, mapping, layout staff, error handling, etc).
I consider that for working in OOP the Project should be enough stable (the concept) at the beggining or it'll be a headache... but I might be wrong.
Anyway I never use large OOP coding before so I'm very slow writing it and far from being efficient.
You have answered the question for yourself. You know procedural and are not very familiar with OO -- therefore you prefer procedural.

Certainly there is an immense amount of information around the web that extols the purported benefits of OO. Either you have realized those benefits in your own programming or you have not -- that's the bottom line.

However, a the question you might ask yourself: Honestly, why are you asking about OO when you currently are efficient in procedural? How you think you "might be wrong?"
(#10850)
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post by Jaxolotl »

As feyd said, many of the questions I've asked find an answer on both threads he provide us. TNX
viewtopic.php?t=37995
viewtopic.php?t=30746

well...
ole wrote:My advice to you Jaxolotl is not to ask people "do you prefer oop or procedual" because you will just get opinions, and strong ones at that. You won't really get anywhere.
hummm first of all I have to say that I find a lot of hi quality information on this forum so I respect the opinion of this comunity. Then I'm good enough as a programmer to understand when an opinion is based on a solid experience or just an"opinion" even if I'm not good enough to understand the argument completly.
Another point is that I'm just a 5 years old programmer but a 23 years old professional musician and I can confirm that a the opinion of professionals are allways a treasure in any kind of profession because is the synthesis of years of experience and for sure a personal point of view of the argument...so it gives a 'multidimensional' approach to the "thing"
ole wrote:You should learn oop, and properly (read about refactoring and design patterns), before you make a decision. Although, take heed, all the highest paid programmers know how to program with objects.
two interesting points to keep in mind...tnx
arborint wrote:Honestly, why are you asking about OO when you currently are efficient in procedural?
why? because I think that if you know how to do (professionally) the same thing in two ways is probably that you do them faster and better separately and for sure you will have a global knowledge that may let you find a third new way of doing it.
arborint wrote:How you think you "might be wrong?"
Is a key to stay away of sleeping the comfortable dream of self satisfaction and keep learning ;) .
I just like to say "what I know is true for me but is not THE TRUTH"
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Honestly, why are you asking about OO when you currently are efficient in procedural?
Same applies to anything that is a fundamental change in style in programming. Why write unit tests when you are programming fine without them? It'll tell you why -- because you could be doing it better and sooner or later you are going to find flaws in your current methodology.

I didn't realise how much I needed unit tests until I learnt what they were and how to do them. In exactly the same way as oop you don't fully appreciate unit testing until you are doing it yourself.
timclaason
Forum Commoner
Posts: 77
Joined: Tue Dec 16, 2003 9:06 am
Location: WI

OOP

Post by timclaason »

To me, procedural programming is just a step in the learning of programming, prior to learning OOP.

To me, an analogy would be procedural programming is to OOP what algebra is to calculus
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

ole wrote:It'll tell you why -- because you could be doing it better and sooner or later you are going to find flaws in your current methodology.
Exactly ... but the real reason people learn something new is not because of what someone else tells them ... it is when they need solutions that they can't easily solve with their current toolbox.
(#10850)
User avatar
The Phoenix
Forum Contributor
Posts: 294
Joined: Fri Oct 06, 2006 8:12 pm

Post by The Phoenix »

arborint wrote:Exactly ... but the real reason people learn something new is not because of what someone else tells them ... it is when they need solutions that they can't easily solve with their current toolbox.
This is dead on.

You start off with a hammer, and everything looks like a nail. Then you run into a screw, and well, the hammer doesn't work as well. Suddenly, you discover screwdrivers, and they are amazing. Then you run into another nail, or worse, a rivet..

Over time, you will develop new ways of dealing with problems. You'll start with "what works", progress to proven methodologies, and may even end up using design patterns.
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post by Jaxolotl »

Spending 8-9 hours a day doing the same thing without trying to improve myself sounds like suicide for me...isn't it? I don't wanna be a zombi LOL.
I think that if you have passion when programming you probably love challenges.

by the way The Phoenix, I use the hammer many times with screws....just to see what happens LOL using hammer to stick two woods with a screw sometimes could work (for a while) but unscrew it with the hammer would be a miracle LOL ;)

PS. i should improve my english
Post Reply