1. When And Why Should I Use Static Functions?

    Now I am by no means the best developer, not the smartest or even the cleverest. I learn new things every day and this is one of the things I learnt some time back that I want to revisit. When I first started programming using Classes I heard about Static Functions but never used them. Yesterday I once again used a Static Function and it struck me that I done so without a second thought.

    Like many other programmers who are still learning what many would consider "core skills" I am always eager to try new things in my code, without my thought to if I require it. Part of being a better programmer is knowing when to use the right tools and techniques. The reason for this lengthy introduction is because I have fallen into the trap where I have a hammer and everything looks like a hammer.

    Onto Static Functions, now I assume you are aware of the basics of a Class in programming. For this article I will post any code in PHP however the idea and reasons remain the same for almost all Object Oriented Programming Languages.

    First to make use of a function inside a Class you are required to declare the Class (normally with a new statement like so.)

    $fish = new Fish();
    

    After that you have the class (also known as the object.) in your code and accessible. From here you have access to all public methods and functions within that class, because you have declared it. However lets say you want to check if the name of the fish is already taken. For example:

    $fish = new Fish();
    $fish_name_free = $fish->is_name_free('carp');
    if($fish_name_free) {
        echo 'party';
    }else{
        echo 'do not worry, there are plenty more fish in the sea!';
    }
    

    Now the code above can easily be improved in a number of ways (for one there really is no need to assign the return of is_name_free to a variable.) however this is a simple example which shows that sometimes creating a new object or class is overkill. What if I told you we never make use of the fish object / class ever again in that section of code?

    There really is no need for use to declare the new class /object for such a simple task. Now this is just one example. How about this one:

    $endangered = FALSE;
    $fish = new Fish();
    $fish->load_fish('carp');
    $locations_of_fish = $fish->all_locations();
    foreach ($locations_of_fish as $location) {
        if($location->is_endangered()) {
            $endangered = TRUE;
        }
    }
    return $endangered;
    

    Again this code can be improved and really is just proving a point. With the code above all the code we are using can be inside the class, inside protected functions. Do I really need access to all functions I have listed? What if I need to do this pattern in multiple places? No good creating a new function for it as I still in to instant the class / object every time.

    A Static Function allows you to make use of the class without ever declaring the class. Now if you need to interact or use the class in multiple ways a Static Function should be avoided. However in both cases I displayed really all I need is a TRUE or FALSE back from the class. I am not going to use the class anywhere else in that script. It is just a one off reference. Also what if I need to use these scripts in multiple places? If I change how my class works I could break a lot of stuff.

    Here is the first example but as a static function.

    $fish_name_free = Fish::is_this_name_free('carp');
    if($fish_name_free) {
        echo 'party';
    }else{
        echo 'do not worry, there are plenty more fish in the sea!';
    }
    

    Again that code can be improved but as a basic easy to reuse line it makes sense. And now the second script as a static function, even less this time around as we would move all the code to inside the class.

    return Fish::is_endangered('carp');
    

    That's it. Now you can reuse that piece of code without worrying about making future changes. Now note that I did not reference any updating, deleting or editing of data in my examples. Generally speaking a Static Function is very handy if you need minimal data back and don't want to be rewriting large chunks of code everywhere.

    Now the temptation is to simply have a Static Function that returns the object but doesn't create the class. So basically you can access all the variables the class would normally set but none of the functions. Unless you really have to lock down your code and don't want people using your functions this is not a good idea. You will end up limiting yourself unless you have a very good reason to use the Static Function.

    Some examples of Static Functions I have seen range from simply performing checks if something is TRUE or FALSE and even the crediting of customers' accounts, if there is an error the Static Function just returns and exception prompting the client to get in touch with the develops (because there is very little that could go wrong.) the reason it was in a Static Function was so it could easily be called and if any changes were made to the class it would not affect the customers getting their credits, because that is still called in the same way.

    Anyway I hope you find Static Functions as exciting as I do!

    You filthy comment whore, you love it don't you?
    Lets not be forgetting to +1 it now... I am tracking your IP...


RSS Feed