Welcome to WebmasterWorld Guest from 3.93.74.227

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Function or Class.which and why?

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

Junior Member

5+ Year Member

joined:Dec 4, 2009
posts: 186
votes: 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
5:07 pm on Feb 28, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2009
posts:1397
votes: 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
5:33 pm on Feb 28, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 4, 2009
posts: 186
votes: 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."
6:38 pm on Feb 28, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 22, 2009
posts:1397
votes: 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
11:02 pm on Feb 28, 2011 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


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.
12:07 am on Mar 1, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 1, 2003
posts:815
votes: 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.
1:34 pm on Mar 1, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 4, 2009
posts: 186
votes: 0


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