CKTIS / PHP
__call
Publiczna metoda `__call` przechwytuje wywołanie brakującej lub niedostępnej metody obiektu.
Wersja: PHP 8.4; repository runtime, production runtime not attested
Składnia
public function __call(string $name, array $arguments): mixedPubliczna metoda __call przechwytuje wywołanie brakującej lub niedostępnej metody obiektu. Otrzymuje nazwę z zachowaniem pisowni oraz tablicę argumentów, w której argumenty nazwane zachowują klucze. Dostępna metoda wykonuje własny kod.
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",[]]]
Przykłady
<?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()]);
Wynik
["direct",["MiXeD",{"0":2,"unit":"kg"}],["secret",[]]]