Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

__call

The public instance method `__call` receives the requested spelling and arguments when an object method is missing or inaccessible in the caller’s scope.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

public function __call(string $name, array $arguments): mixed

The public instance method __call receives the requested spelling and arguments when an object method is missing or inaccessible in the caller’s scope. Positional arguments have numeric keys and named arguments keep their names. An accessible declared method bypasses this fallback. The handler can return a result or throw for an unsupported operation.

class Switchboard {
    public function ping(): string { return "direct"; }
    private function secret(): string { return "inside"; }
    public function __call(string $name, array $arguments): mixed {
        return [$name, $arguments];
    }
}
$b = new Switchboard();
echo json_encode([$b->ping(), $b->MiXeD(2, unit: "kg"), $b->secret()]);
["direct",["MiXeD",{"0":2,"unit":"kg"}],["secret",[]]]

Examples

<?php
class Switchboard {
    public function ping(): string { return "direct"; }
    private function secret(): string { return "inside"; }
    public function __call(string $name, array $arguments): mixed {
        return [$name, $arguments];
    }
}
$b = new Switchboard();
echo json_encode([$b->ping(), $b->MiXeD(2, unit: "kg"), $b->secret()]);

Result

["direct",["MiXeD",{"0":2,"unit":"kg"}],["secret",[]]]

Sources