Function vs Class

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Which do you prefer...

Functions
19
32%
Classes
40
68%
 
Total votes: 59

Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

I use 'standard' functions in PHP more or less all the time but it doesn't mean the code is messy or can't easily be changed. I have a collection of seperate PHP files each containing a function.. an email sorting and sending function for example.. and simply include() the functions if and when they're needed. This way only the code that is required will be included in the pages.. which helps to keep the speed up.
lastcraft
Forum Commoner
Posts: 80
Joined: Sat Jul 12, 2003 10:31 pm
Location: London

Post by lastcraft »

Hi...
BDKR wrote:You now Marcus, that was a rather simple black and white response.
Sorry, I did not intend to be rude and apologies if I was :(. Consider the pace of writing enthusiasm rather than aggression...please.
BDKR wrote:I'm presently digesting the QIII engine, which is written in C FYI, and it's not tough to extend or change. Not to the point you make it out to be.
Well, I am first and foremost a C programmer so I know what you mean. Now C has functions as first class objects (albeit with pointers and the C type system) which makes it possible to do OO or functional programming to some extent. The fact that C can be bent this way does not make it the best solution, however. By comparison C++ is a hideously complicated language which makes C preferable for small projects. It becomes worth the extra expense of C++ developers when the code base gets beyond 30000 lines or so (in my experience). If you like the extra skill cost balances the the large project leverage you get from OO. I am not saying that it is impossible to write flexible systems with non-OO languages or less than fully functional languages, only that there is a cost involved in reduced flexibility. It is simply more work.
BDKR wrote:...as much as the C language will allow that is.
But bending a non-OO language into an OO one simply demonstrates the desirability of that quality. I have seen C chosen most often because of the lack of C++ experience (I do this too) or because of a lack of knowledge of more radical solutions (a domain language say) not because it is prefereable to use the simpler paradigm.

Quake 3 is a good example of this, adding it's own partly declarative language on top of the C core. By changing language the number of lines of code is kept below the number that triggers the need for strong encapsulation.
BDKR wrote:This isn't as clean as you portray it too be.
OO does take a couple of years to get a handle on, but at that point I disagree. It is very much as clean as I make it out to be. The example I quoted was a grand total of three hours work (including tea breaks). Now the example was very suited to OO (no relational database involved for example) I grant you, but I am now well into the domain where my 3 years OO is more productive here than my seven of non-OO.
BDKR wrote:You've just raised the same old battle cry that other dedicated OOP'rs out there do, and you've cried it out in the same old simplistic fashion.
Hm...I am in the OO camp for the PHP apps I do now, but I was not out to expouse OO as such. Simply I wanted to point out that rejecting OO for performance reasons was a bad choice. There are reasons for rejecting OO, mainly when encapsulation is going to cause trouble. For example if there is a large amount of configuration or you are interfacing with a system that uses it's own language or there is very little state in the system. All of these will cause development overhead if done with OO.
BDKR wrote:OO just makes it easier to attain a clean, modular, easy to maintian code base.
Which are exactly the qualities that you most need for performance tuning.
BDKR wrote:Cheers,
BDKR
Thanks for not letting me get away with it so easily! :)

yours, Marcus
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Hm...I am in the OO camp for the PHP apps I do now, but I was not out to expouse OO as such. Simply I wanted to point out that rejecting OO for performance reasons was a bad choice. There are reasons for rejecting OO, mainly when encapsulation is going to cause trouble. For example if there is a large amount of configuration or you are interfacing with a system that uses it's own language or there is very little state in the system. All of these will cause development overhead if done with OO.
OK. I see what you're getting at. My outlook isn't so much one of 'code-smithing' (a cool term that I'll have to remember), though I've been known to tinker and try some crazy things here and there, but instead, to do what I know performs better up front. Then, when I feel the size of the project justifies incurring the performance hit, I'll go OO.

But oh well...., it's all good. I'll prolly hit you up with some questions concerning C. :wink:

Cheers,
BDKR
evilMind
Forum Contributor
Posts: 145
Joined: Fri Sep 19, 2003 10:09 am
Location: Earth

oop not oops :)

Post by evilMind »

OOP is the best way to go. Especially when php5 comes out ( since there will be major changes). See http://www.php.net/zend-engine-2.php for the new features. OOP takes a little more programming at first but once you get it to do what you want it runs like a well oiled machine...
User avatar
cybaf
Forum Commoner
Posts: 89
Joined: Tue Oct 01, 2002 5:28 am
Location: Gothenburg Sweden

Post by cybaf »

OO or nono...:) I'm all for OO even with the extra amount of coding... seams weird maybe because a good programmer is a lazy programmer... but the time you put in building nice reusable classes will spare you a lot of time in the long run.

//cybaf
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

voted for functions, i have't really found a good use, but i do make a class once a while to stay in shape lol...
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Writing a class and writing an Object Oriented program are two different things. Not to anyone in particular, I just want this to be clear.

The fact that you use classes in your program does NOT make your program object oriented.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

I voted for functions because that's what I use with PHP. I like to write tight, fast code for my web projects. I don't feel that using classes helps me write tight, fast code.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Using "speed" as an argument not to choose for OOP is most of the times just silly.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

timvw wrote:Using "speed" as an argument not to choose for OOP is most of the times just silly.
I'll just refer you to Jason's last statement just above. :)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Jason was talking about procedural v OOP code not speed.

Lastcraft nailed the often-stated (but mistaken) speed issue, I think.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

One of my favorite posts on the whole procedural vs OOP thing...

http://www.sitepoint.com/forums/showpos ... stcount=14
Zay
Forum Newbie
Posts: 22
Joined: Tue Mar 23, 2004 3:42 pm

Post by Zay »

I never had an idea that required classes to make it better...

I worked with classes with Delphi and Java, so know what I can use them for... I just haven't had much that required classes on PHP... but it will most likely change with my next idea that I will be working out in a few months...
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

jason wrote:Writing a class and writing an Object Oriented program are two different things. Not to anyone in particular, I just want this to be clear.

The fact that you use classes in your program does NOT make your program object oriented.
Good point. I have seen code in the past using classes, just for the sake of saying they used classes. Not a valid reason.

Mark
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

ive never used classes, but have downloaded many, which unfortunately ive never used i do think that they are bloated just by looking at the size of the script

i have started to use functions, and cant praise them enough :wink:
Post Reply