If you call a non-statistical function with static syntax inside any class (and not just the declaration class), then the non-static function behaves as if it was called in the context of the current $ this object.
This means that I can use methods from another class inside my class, and these methods will consider my $ this class to be its $ this class. If they do not check InstanceOf or get_class() , they will behave exactly as usual.
When I say as usual, I mean that they will assume that your $ this class has all the other methods and properties that their $ this class will have.
This (I think) is called object context binding. In JavaScript, you must use the call() and / or apply() methods to make the function behave as if it were bound to a specific object. But in PHP, it just works using static syntax for non-static functions.
I remember that this works in C ++ as well, but I canβt remember if it only works if the methods belonged to inherited classes (possibly overridden methods that you want to access, even though they redefined).
Using this (hidden) function, you can implement the Decorator design template, with which you add new functions to the object at runtime. You can also simulate Mixins or Traits (which PHP 5.4 now supports) with an added bonus that this implementation is processed at runtime and as such can be executed / canceled dynamically, unlike the implementation of a language function that is processed at compile time and cannot to be executed / canceled dynamically, you need to change the code to change the function.
source share