How laravel can inject function to object in their ORM
2024-11-17
The technique they have used are “trait“, “magic function” and “call_user_func_array“. The whole thing work in runtime, not compile time.
1. Php trait
PHP trait is just like copy and paste the code from trait into the injected class.

2. Magic function
When calling a not exist function, the __call function will be called. Laravel is using library Macroable, so in the code, when you are calling a non exist function on an object, the __call will be called and try to call the real function by call_user_func_array
