在 PHP 物件導向開發中,若嘗試呼叫類別中未定義的方法,程式會拋出 Fatal error: Call to undefined method 並立即終止執行。這在動態方法呼叫或 API 介面設計時極為不便。
__call() 會在呼叫**不可存取的方法**(不存在、權限不足)時自動觸發,接收兩個參數:
$method:被呼叫的方法名稱$args:傳遞給該方法的參數陣列<?php
class Test {
public function __call($method, $args) {
echo "嘗試呼叫不存在的方法:$method() <br>";
echo "傳入參數:";
print_r($args);
echo "<br>程式繼續執行...<br>";
}
}
$obj = new Test();
$obj->demo("one", "two", "three"); // 不存在的方法
echo "這行程式碼仍會執行!<br>";
?>
輸出結果:
嘗試呼叫不存在的方法:demo()
傳入參數:Array ( [0] => one [1] => two [2] => three )
程式繼續執行!
<?php
class ServiceProxy {
private $service;
public function __construct($service) {
$this->service = $service;
}
public function __call($method, $args) {
if (method_exists($this->service, $method)) {
return call_user_func_array([$this->service, $method], $args);
}
throw new BadMethodCallException("方法 $method 不存在");
}
}
?>
<?php
class ApiClient {
public function __call($endpoint, $params) {
// 統一處理所有 API 端點呼叫
return $this->request($endpoint, $params[0] ?? []);
}
private function request($endpoint, $data) {
// 實際 HTTP 請求邏輯
}
}
$client = new ApiClient();
$client->getUser(['id' => 1]); // 對應 GET /user
$client->createOrder(['item' => 'A']); // 對應 POST /order
?>
__call() 僅處理**物件實例**呼叫的非靜態方法__callStatic() (PHP 5.3.0+)__call() 是實現動態行為、建立流暢介面、處理 API 代理的強大工具。善用它能讓程式碼更具彈性,但也要注意不要過度隱藏邏輯,影響程式碼可讀性。