General Community > Chit Chat

Function VS. Class

<< < (3/6) > >>

Shoeb Omar:
Ok, so I'm a newb.

What's the difference?

mattsiegman:
for example, in Perl

function style:

--- Code: ---#!/usr/bin/perl

&do_stuff;

sub do_stuff { return; }

--- End code ---

OOP

--- Code: ---#!/user/bin/perl
use Stuff;
my $stuff = new Stuff;
$stuff->do;

package Stuff;

sub new { return bless shift ; } # I don't think that's legal, but I'm lazy
sub  do ( return; )

--- End code ---

normally, if the example is longer, this would be better, but you can see the different sytle

Zef Hemel:
In the OO paradigm you think of everything more like in reality, as objects. In a forum you have different classes of objects: Users, Topics, Boards, Categories and Messages. Every action related to a certain class of objects is put in that class. $user->canAccess($board), $user->isAdmin(), $board->addTopic($topic), $topic->addMessage($message).
What I use for a project of mine is a class called DataObject, which can through reflection (a mechanism to dynamically obtain which fields and methods are in the object) can add itself, update itself, load itself and delete itself from the database. When I created that object, I could just create subclasses of DataObject that extended that behaviour: Employee, Job, Project, without having to code it all over again:

--- Code: ---$employee = new Employee();
$employee->loadFromId(5); // Would've prefered a static method, but PHP4 doesn't support that
$employee->name = 'Zef Hemel';
$employee->update();
--- End code ---
No more SQL queries and lot cleaner code.

The fact that it's a bit slower is not important in my opinion as you know Unknown. Programming is about more than just performance and as I said many times before, if performance is all that matters you shouldn't be programming in PHP any way, use C or assembly instead. In the real world nobody will accept "Development will take three times as long, but it saves us about 50ms per page view!"

[Unknown]:

--- Quote from: Zef Hemel on August 16, 2003, 05:33:12 AM ---The fact that it's a bit slower is not important in my opinion as you know Unknown. Programming is about more than just performance and as I said many times before, if performance is all that matters you shouldn't be programming in PHP any way, use C or assembly instead.
--- End quote ---

I think we both know it's not practical to do so - many servers run linux, and having different binaries, etc.... plus it wouldn't work except inside the cgi-bin... it'd be a pain.


--- Quote from: Zef Hemel on August 16, 2003, 05:33:12 AM ---In the real world nobody will accept "Development will take three times as long, but it saves us about 50ms per page view!"

--- End quote ---

I'm not asking anyone to.  I don't need OOP to be able to understand my or others' code, or to write properly geared functions.  For some reason I'm able to do this *without* the help of OOP... why might that be?

Oh, right - it's because I'm what's called a programmer.  If I wasn't one, I probably would *need* OOP to do my job well.


That said, I'd like to make a distinction.  I think RELYING on OOP the way so many do is a bad idea... however, in ym own scripts I do use a bit of OOP, for the purpose of database abstraction (simple, low level, not the way you like it.) and user management.  I do not *need* this - it just simplifies things without making them needlessly slower.
And in these ways it actually does simplify things - there's no way to do abstraction well without abstraction.  (the only other way would be like $sql_query = 'mysql_query'; $sql_query('SELECT 1');...)  And doing it with a class is not significantly slower than otherwise.

Yet, this isn't OOP to you - to you OOP is making everything objects and abstractifying them.  Why?  To make everything useless and slow.

-[Unknown]

Zef Hemel:

--- Quote from: [Unknown] on August 16, 2003, 06:37:35 AM ---I'm not asking anyone to.  I don't need OOP to be able to understand my or others' code, or to write properly geared functions.  For some reason I'm able to do this *without* the help of OOP... why might that be?
--- End quote ---
Why might that be? Who said that OOP is the only way to write understandable code?


--- Quote ---Oh, right - it's because I'm what's called a programmer.  If I wasn't one, I probably would *need* OOP to do my job well.
--- End quote ---
???

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version