homepage Welcome to WebmasterWorld Guest from 54.196.159.11
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Function or Class.which and why?
IntegrityWebDev




msg:4273909
 4:02 pm on Feb 28, 2011 (gmt 0)

I'm a self taught PHP guy and I've worked on many small projects where I have created a "functions.php" file to store common functions that I will be doing on every page, such as a DB connection. Then at the top of the page I just include the functions file and call the function. These have always been small projects.

Now I'm working on a "BIG" project where things like structure and efficiency really matter...and my deficiency in understanding these areas is shining through.

So my question is this...lets say I have a lot of basic functions for things like connecting to a DB, testing a cookie against something stored in the DB and so forth...things I will need to do on almost every page. Should these be put in a functions file, or should these functions be put in a class. Which and why?

Thanks!
Chris

 

Matthew1980




msg:4273946
 5:07 pm on Feb 28, 2011 (gmt 0)

Functions are fine for little operations, you could pop these into something called "generic class" so that they are the more common in operation, then for the more intrinsic things you may need a dedicated class to handle the specifics, this is how I have always viewed this.

But as there are a lot of people on here with methods of their own, there are bound to be lots of differing opinions over this. But, it's your choice, and this is where you find out which will suit your particular project.

Sorry I'm not any clearer on this, but classes are difficult things to get your head around, but once the logic/structure penny drops you will see how powerful classes actually are.

There is no harm in having a file full of functions at all, I still advocate this, and they are quite handy, and sometimes classes can get a bit spaghettified which is sometimes unavoidable!

Cheers,
MRb

IntegrityWebDev




msg:4273968
 5:33 pm on Feb 28, 2011 (gmt 0)

Good post Matthew! Hope to get a lot more feedback here.

Maybe a good rule of thumb is "If you can do it in under XX lines of code, put it in a function file...if not, put it in a class."

Matthew1980




msg:4274019
 6:38 pm on Feb 28, 2011 (gmt 0)

>>Maybe a good rule of thumb is "If you can do it in under XX lines of code, put it in a function file...if not, put it in a class."

Wow! that's spooky, I have just had a discussion with one of our senior developers (java/c#) and we arrived at the agreement that if your coding/programming a general rule of thumb is: if you type a routine out more than once, it should be a function.

And if you do something that is over ~25 lines, you could probably split it up into smaller functions; but I have to disagree with her on that last one! It depends on the context of what your doing.

Yes, I too am looking forward to more posts here, to see what sorts of opinions are out there.

Cheers,
MRb

coopster




msg:4274260
 11:02 pm on Feb 28, 2011 (gmt 0)

I keep a functions file on hand for methods that are not pre-defined functions in PHP but that I need to use on every page. I typically auto include it then too.

timster




msg:4274288
 12:07 am on Mar 1, 2011 (gmt 0)

And if you do something that is over ~25 lines, you could probably split it up into smaller functions; but I have to disagree with her on that last one! It depends on the context of what your doing.

I'd listen to her. A function should do one thing only. Shorter functions make much more maintainable and flexible code.

Should these be put in a functions file, or should these functions be put in a class.

If you understand object orientation, then creating classes for this sort of functionality will create cleaner, more maintainable, more reusable code. You have worked on small code projects, where you have probably been able to keep everything in mind at once. Once your functions file grows to 10,000 lines or more, you will see what I mean.

That said, if you do not have a grounding in object orientation, then I wouldn't try creating classes ad hoc. You are likely to make a real mess. That is, unless your senior developer can provide guidance to keep you out of trouble.

IntegrityWebDev




msg:4274551
 1:34 pm on Mar 1, 2011 (gmt 0)

Thanks for all the input. Good thoughts, good discussion, good info!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved