| 1 | <?php |
||
| 12 | trait Macroable |
||
| 13 | { |
||
| 14 | use BaseMacroable { |
||
| 15 | BaseMacroable::__call as macroableCall; |
||
| 16 | BaseMacroable::__callStatic as macroableCallStatic; |
||
| 17 | } |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Handle dynamic method calls into the model. |
||
| 21 | * |
||
| 22 | * @param string $method |
||
| 23 | * @param array $parameters |
||
| 24 | * |
||
| 25 | * @return mixed |
||
| 26 | */ |
||
| 27 | public function __call($method, $parameters) |
||
| 28 | { |
||
| 29 | if (in_array($method, ['increment', 'decrement'])) { |
||
| 30 | return $this->{$method}(...$parameters); |
||
| 31 | } |
||
| 32 | |||
| 33 | try { |
||
| 34 | return $this->forwardCallTo($this->newQuery(), $method, $parameters); |
||
| 35 | } catch (Error | BadMethodCallException $e) { |
||
| 36 | if ($method !== 'macroableCall') { |
||
| 37 | return $this->macroableCall($method, $parameters); |
||
| 38 | } |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Handle dynamic static method calls into the method. |
||
| 44 | * |
||
| 45 | * @param string $method |
||
| 46 | * @param array $parameters |
||
| 47 | * |
||
| 48 | * @return mixed |
||
| 49 | */ |
||
| 50 | public static function __callStatic($method, $parameters) |
||
| 51 | { |
||
| 52 | try { |
||
| 53 | return (new static())->{$method}(...$parameters); |
||
| 54 | } catch (Exception $e) { |
||
| 55 | if ($method !== 'macroableCallStatic') { |
||
| 56 | return (new static())::macroableCallStatic($method, $parameters); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | } |
||
| 60 | } |
||
| 61 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: