Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question regarding class with static functions and namespace in PHP

Tags:

php

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.

like image 909
jack97 Avatar asked Oct 25 '22 13:10

jack97


1 Answers

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.

like image 164
Christian South Avatar answered Oct 27 '22 11:10

Christian South