我有很多方法需要测试远程服务器是否已经到达,如果没有,达到它。在另一个之前调用方法
我的第一个想法是__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