December 28th, 2012, 03:36 PM
Global functions loading?
I have been reading quite a lot today (ie, this ) and something confuses me. If I am right, using Static classes as tools is probably wrong. Instead, using global functions might be better. But unlike Global functions Static classes (as tools) can use the autoloader. So how would one efficiently load global functions? Would one maybe create a sort of autoloader for these functions.
Could anyone give some advise (a good practice for loading global functions)?
Last edited by aeternus; December 28th, 2012 at 04:11 PM.
December 28th, 2012, 05:08 PM
don't let this confuse you. There's nothing wrong with utility classes. You should indeed be careful not to overuse them, but I'd prefer some tightly structured classes over a bunch of loose global functions any time.
Java and Ruby, for example, both have a Math class/module with static methods like "sin" or "exp". And in my opinion, that's the only right way to do this.
Of course you might argue that PHP itself is just a bunch of loose functions and that using a clean class structure would somehow break this design "principle", but I find that kind of odd. So just keep your utility classes.
December 28th, 2012, 05:33 PM
Thanks for your feedback. I am fairly new to oop so I read a lot and came across this. There is a lot of debate going on so this confuses me
A bit off topic (although a bit related): I had the same with the global variable. I am searching my Butt off for alternatives (because it's supposed to be bad). I noticed the use of the global variable in Wordpress (global $current_user; (which is an instance of WP_User) I suppose that might be bad because someone could create a variable with that exact name? An alternative could maybe be to use the scope resolution operator :: ?
not that I need help with this worddpress, but more if teh idea is correct that a global variable which is an instance of an object could be called also with the scope resolution operator
$current_user = WP_USER::wp_get_current_user();
Atleast oop is not boring to learn haha
Last edited by aeternus; December 28th, 2012 at 05:39 PM.
December 28th, 2012, 07:42 PM
Well I only use static properties and methods in the Registry class, which stores some important objects such as Cookie, Session and the current User. Id say its your best shot to avoid using statics as much as possible since they aint good OOP practices, but there are circumstances you may actually need them badly.
December 28th, 2012, 08:09 PM
The autoloading question is one of the best arguments I know in support of using "static" classes in PHP, and is one of the very few reasons I would find them acceptable. It's arguably bad practice but sometimes you have to bend the rules to work with the language.
With that said, I bet 99.9% of the functions you want could go just fine in some existing class. Probably static. The $current_user is one example: it could be a global variable, and it could be returned from a global function, but it belongs just fine in some WP_USER class so that's the best place to put it.
December 29th, 2012, 07:46 AM
Thanks Hall of Famer and Requinix for the response,
I will try to limit the amount of static classes. Interesting though almost all CMS and packages I came across use a whole bunch of utility classes with static methods. (joomla, wordpress, swiftmailer etc) I'll probably have to read a bit more and make a real live example and see what works best.
Just wondering though you say
This kind of confuses me, If you put a static function in a class, doesn't that make it more of a utility for outside that class by using that scope resolution operator. If it weren't static it can use the $this variable if I am right. So why would 99% be static in the class if it is tied to that class? I am probably missing something
Can I maybe use a rule of Thumb that If I would like to use a property or method outside the class (with out instantiating the object) I should make it static?
-edit i found this article that seems to give a summary at the end, but I can't really see why a static variable or function would differ from a global one. (except for its position)
Last edited by aeternus; December 29th, 2012 at 08:18 AM.