#1
  1. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115

    Global functions loading?


    Hi guys,

    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 03:11 PM.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    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.
  4. #3
  5. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    Hi Jacques,

    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 :: ?
    PHP Code:
    $current_user WP_USER::wp_get_current_user(); 
    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

    Atleast oop is not boring to learn haha
    Last edited by aeternus; December 28th, 2012 at 04:39 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Ithaca
    Posts
    68
    Rep Power
    2
    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.
  8. #5
  9. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,119
    Rep Power
    9398
    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.
  10. #6
  11. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115
    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
    With that said, I bet 99.9% of the functions you want could go just fine in some existing class. Probably static.
    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 07:18 AM.

IMN logo majestic logo threadwatch logo seochat tools logo