Dynamic method calling in PHP using variables

A very naive yet effective approach on metaprogramming that helps you dynamically call the right method in a very concise manner.

```php
$method = $user instance of User ? 'save' : 'saveMany';

$this->members()->$method($user);
```

The example is an excerpt from a dummy Laravel app.

Nick Ciolpan
30 Sep 2020
« Back to post