# Dynamic method calling in PHP using variables

> How to dynamically call a method in PHP by using variables as the method name. This trick will save you a lot of time and conditional clauses.

- URL: https://graffino.com/til/a-concise-conditional-method-calling-in-php-using-variables-for-method-names
- Author: Nick Ciolpan
- Channel: php
- Published: 2020-09-30
- Updated: 2021-06-30

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.
