I have bunch of methods in which i need to test whether remote server has been reached or not and if not, reach it.
My first idea was __call magic method, but the method is called only when real method (with the original name) is not presented.
<?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;
}
?>
Do I really need to have bunch of if's in each method or there's a way how to do that better?