我有很多方法需要測試遠程服務器是否已經到達,如果沒有,達到它。在另一個之前調用方法
我的第一個想法是__call魔術方法,但該方法被調用只有當真正的方法(與原來的名稱)沒有提出。
<?php
public function __call($name, $arguments) {
$needsExecution = array(
'getBody', 'getHeader', 'getHeaders', 'getRawOutput',
'getStatusCode', 'getFullHttp'
);
if (in_array($name, $needsExecution)) {
if (!$this->hasBeenExecuted()) {
$this->execute();
}
}
}
public function getBody() {
return $this->responseBody;
}
public function getHeaders() {
return $this->responseHeaders;
}
?>
我真的需要在每種方法中都有一堆if,或者有一種方法可以做得更好嗎?
是的,它是合法的:'$ this - > {'_'。$ funtionName}()' – webbiedave
謝謝,重新加入。 – sberry