开发者

Question regarding class with static functions and namespace in PHP

开发者 https://www.devze.com 2023-02-27 23:46 出处:网络
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?

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 开发者_如何学Gothis 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消