Muhammad Usman

Crazy tech lover, endless learner
Home

Laravel 4 Uses Lots of Static, Not True.

Most of the time developers blame Laravel for using too much static. This claim is not true in case of Laravel 4, let’s dig in and discuss why and how.

What is static?

In PHP you call it like this Class::$property.

As the name suggests, it preserves the state. Static variables preserve their values across the lifetime of the running application(in our case request). In a class if a static property is declared it becomes the property of the CLASS itself, NOT the property of the instance(s) of the class.

Why is static considered bad?

Well, there are debates. One of main reasons is, you can’t (quite) Mock a static variable or method as its difficult to override statics because the state is preserved and no object/instance is used. This makes it really difficult to Test(Unit Test) an application as Mocking is a must needed thing to unit-test an application fully.

When you start unit-testing your application then you will realize why static dependencies are so bad. It becomes quite impossible to replace implementations during testing. So you end up creating lots of hacks just for maintaining testability.

How does Laravel static work?

Laravel 4 doesn’t actually use static rather it mimics the static calling behavior. When you call a static method, actually much more things happen behind the scene than it appears. Let’s go through the life cycle of a static call.

As an example we’re calling View::make('hello').
You will see literally there is no class View under the `` namespace. Rather Laravel uses aliases, so when you look at app/config/app.php, you will see View is just an alias of Illuminate\Support\Facades\View. So you’re actually calling:

   
Illuminate\Support\Facades\View::make('hello');  

When we go to Illuminate\Support\Facades\View under (vendor/laravel/framework/src) we see just this code:

   
class View extends Facade {

    /**  
    * Get the registered name of the component.  
    *  
    * @return string  
    */  
    protected static function getFacadeAccessor() { return 'view'; }

}  

It extends the base Facade class, and in both classes there is no static method make(). The magic happens on the base Facade class, where there is a magic method __callStatic. This is called PHP Overloading, whenever you call a static method and this class doesn’t have that method then __callStatic is called(like a 404 page) with the called method name(‘make’) as the first parameter and the called parameters’ array as second parameter(array(‘hello’)).

What __callStatic does basically here, it calls the IoC Container binding with the name ‘view’ and calls the (non-static)method (‘make’) within it. What IoC Container contains, a pure instance of the original View class, in an object oriented way (not a class oriented way). So we can re-write the View::make('hello') as

   
$app->make('view')->make('hello')  

Conclusion

Now we know Laravel makes it super easy to call methods which makes it look like static but actually it’s not, so we can easily mock the original View class and get our tests pass.

Using these facades for static call is just a way to access Laravel classes, you can write your entire app without using these facades(static calls).

Do you find anything incorrect, do you have something better to share? Please write in the comments.