PHP - is this bad practice?

Soldato
Joined
8 Oct 2005
Posts
4,184
Location
Midlands, UK
Hi,

For a lot of my old projects I used a simple class called Tools, with static methods for things like filter input and other basic, common tasks. I'd include this class on the desired page. If I wanted to call one of the classes methods I'd then do Tools::somemethod();

Is this really bad practice? Only reason I ask is because a customers relative is apparently a 'top' php developer and pointed out that uses classes in this way is seriously bad practice. The customer is a bit concerned.

Have I dropped a ball here?

Thanks
 
They're called utility classes, and I don't think anyone worth their salt thinks of them as inherently bad—especially in a language like PHP, which lacks the ability to monkeypatch. Ignore the guy.
 
Tell your customer to stop being concerned and go read Joel Spolsky's blog dated 23 Sept. I do hate this sort of thing. Tell them next time you'll double the cost and do it according to 'best practice'.
 
Nope its not bad practice. We call those types of classes Static Helper Classes.

The only bad thing is it goes against Class ideaology. I.E. a class is supposed to represent a single entity. Eg. A Customer Class.

To get away from this. Your helper functions could just be a group of functions in an SSI.
 
No, it's not bad practice at all. However, as djkav said, it does somewhat oppose OOP principles.

Strictly speaking, the correct way to separate utility functions from the rest of your code is to use namespaces, though these have only been supported since 5.3.

Tell your customer to stop being concerned and go read Joel Spolsky's blog dated 23 Sept. I do hate this sort of thing. Tell them next time you'll double the cost and do it according to 'best practice'.

He almost seems to be advocating writing sloppy code simply to get the job done more quickly. While spending days agonizing over how to organize your utility functions is clearly senseless, good design shouldn't be neglected.
 
Last edited:
He almost seems to be advocating writing sloppy code simply to get the job done more quickly. While spending days agonizing over how to organize your utility functions is clearly senseless, good design shouldn't be neglected.

I disagree. I not saying don't be organised, but he is saying don't over complicate things.
 
Right, have decided to toptally ignore anything this tool says from now on as he's just been on the phone to me. He said he'd been looking over the source code the site and noticed another 'issue' as he put it.

Told me when I'm outputting options for a drop down I should be using a tab (\t) and not just a new line (\n) for each option (yes, he thought this required a phone call). Okay, so the select options are aligned to the far left whern you view the page source from your browser, this makes bugger all difference to anything.
 
That's the kind of thing which requires a red-flagged e-mail with URGENT in the subject line first, I hope he gave you a warning before the followup call?

:(
 
Quick, set up a conference call with all parties involved and debate this meaningful and significant problem of tabs vs newlines.

Hell.. if it were me developing it, he'd be lucky to get any white space at all in the mark-up :p
 
Quick, set up a conference call with all parties involved and debate this meaningful and significant problem of tabs vs newlines.

Hell.. if it were me developing it, he'd be lucky to get any white space at all in the mark-up :p

Hehe :)
 
Back
Top Bottom