We’re having this discussion here. Which is better and why?
1) a php file that just includes a function…
function bar(){}
called like this
bar();
2) a php that contains a class with a static method.
class foo{
static function bar(){}
}
called like this
foo::bar();
Are there any performance reasons to do one or the other? For code organization purposes the static method looks better to me but there are a few programmers here who see no reason to declare classes.
I’d say the main advantage is the reuse-ability and modularity aspect – what if you have two different “libraries” of functions you’ve created
AandB, and they both happen to havebar()functions? As static methods, you can callA::bar()andB::bar(), but as independent functions you’d have name conflicts.Sure, you can get around this by putting weird prefixes on all your independent function names to make it unlikely that names will conflict, but if you’re going to do that, it’s a lot simpler to just wrap it in a class and use them as static functions.
In PHP 5.3+ you can also use namespaces to accomplish this purpose if you so desire.