# PHP Magic Methods: __invoke and __call

While `__construct` and `__toString` are common, PHP offers other powerful magic methods for dynamic behavior.

---

## 1. __invoke()
The `__invoke()` method is called when a script tries to call an object as a function. This allows objects to be treated like closures or callbacks.

```php
class CallableClass {
    public function __invoke($x) {
        var_dump($x);
    }
}

$obj = new CallableClass;
$obj(5); // Outputs: int(5)
```

## 2. __call()
The `__call()` method is triggered when invoking inaccessible methods in an object context. This is useful for dynamic method handling or proxy patterns.

```php
class MethodTest {
    public function __call($name, $arguments) {
        // Note: value of $name is case sensitive.
        echo "Calling object method '$name' " . implode(', ', $arguments);
    }
}

$obj = new MethodTest;
$obj->runTest('in object context');
// Outputs: Calling object method 'runTest' in object context
```

[[programming/php/php]] [[programming/php/php-magic-methods]]