5

I am using PHP 5.2.14 at work, so there is no namespace option for me. Is it acceptable for me to substitute classes with static functions for namespacing?

For example, we have lots of "handy" function lying around that does miscellaneous things that are all bunched into this one file, and they tend to get messy. I would love to see them follow some sort of organizing logic.

So my solution is this... I want to just create classes called "StringTools" or "DateTools" and every time we need to do use those function I would simply call SomethingTools::funciton_name(...). It would be simple class filled with static functions, with an empty constructor, made purely for the sake of namespacing and organizing.

It would be easy to manage and very organized, because related functions will be organized into it's own file or even folder, the class calls will be handled by autoload, so we don't even have to include anything.

Is this an acceptable approach to the problem? or is there a better way to deal with organizing functions in pre 5.3 PHP so programmers doesn't step on each others feet when naming them? unorganized stuff really really bugs me and I'm actually looking forward to the hours of extra work I'll be spending sorting this out.

2
  • 2
    It's acceptable and widely used. (And a proper namespace syntax would have looked so anyway.) Commented Apr 22, 2011 at 22:06
  • 10 years later, I'd personally suggesting creating Traits for the useful tools and splitting them up by purpose. TransformsStringsToArrays, ParsesDatesFromGoogleApi, and so on Commented Jul 14, 2021 at 9:02

1 Answer 1

3

This is not just acceptable its probably the only clean solution. The goal of an object should always be to group like functions, so as long as you are following this, it would make sense (IMO) to group these functions into classes. What you want to watch out for is that you actually grouping like functions and not just throwing functions into a class. This will just make the problem worse as it will confuse developers when it comes to finding methods when writing new code.

Sign up to request clarification or add additional context in comments.

1 Comment

Cool, thanks for the answer. Yeah, I will group like function together, which is the whole point of me doing this. Right now we have a file with almost 10,000 lines of codes of functions dating back to the late 90's, some of which are not even used anymore... The new organized static methods coupled with autoload is going to be pretty damn amazing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.