$smcFunc as a static method via overloading

This was more of something I wanted to play with.  I wanted to convert $smcFunc into a method.  So for instance lets say $smcFunc[‘db_quote’]() would be smcFunc::db_quote().

This wasn’t looking good at first.  I thought I could use __construct and just intercept what is going on.  But static calls do not use __construct.  So this left me thinking of how I can achieve this.  Of course I could just manually setup all functions inside a smcFunc class.  But this would be a lot of work and wouldn’t work with db_extend.

Finally I came across what I need.  It only exists in php 5.3.0+, so this isn’t a solution for everyone.  But for my sites and where this script is going to be ran, I use php 5.3.  I am using __callStatic.  Simply using that, I then just use call_user_func_array to pass it to $smcFunc

Here is all the code needed to do this:

class smcFunc
{

public static function __callStatic($name, $arugments)
{

global $smcFunc;
return call_user_func_array($smcFunc[$name], $arugments);

}

}

Really slick and gets the job done nicely 🙂

Of course the smcFunc class won’t just have that, as I have other plans.  However this is my biggest hurdle I needed to jump.  I am hoping the others will be possible as well.

2 Comments

  1. If you have an example that would be great. During my tests I couldn’t get it to work that way. I could create the object and have it fill a bunch of private variables, however when I call it via a static, it would fail.

    Hmm.. Small reply box.. Need to fix that.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.