I love Laravel, and you? Overall, the Facades feature! When you use a facade like some Eloquent model, you have noticed the following case:

Here, we have two feature very important and useful:

  • the chain
  • the call to the static method where, and the call to the instance method where

Now, the chain is clear. It is very easy to implement. In fact, you need only return the $this pointer in your methods. Let me show an example:

Then, you may use this class in this way:

The hardest part is the implementation of static and instance method. In PHP you can’t use the same name for a static and instance method. This code below will issue an exception:

PHP will respond by an exception Fatal error: Cannot redeclare MyClass::where(). This is easily achieved by using the __call() and __callStatic() magic methods.

Finally, we can use the method where both as static and as instance, along with the “chain”:

Of course, you may add other methods to this class as needed. For example, if you need to implement the turnOn method, just add the following:

That’s all!