是的,這是可能的。那麼,父母無法知道全部是可能的子方法,但它可以通過知道知道什麼時候通過實現__call
magic method來調用未定義的方法。
我們可以使用這種方法動態地創建一個try-catch「包裝器」。
此方法添加到您的父母:
現在
public function __call($method, $args)
{
// If method call ends with 'Safe'
$isSafeMethod = substr($method, -strlen('Safe')) === 'Safe';
if (!$isSafeMethod) {
trigger_error('Call to undefined method '.__CLASS__.'::'.$method.'()', E_USER_ERROR);
return null;
}
// Strip 'Safe' suffix from method name
$wrappedMethodName = substr($method, 0, strpos($method, 'Safe'));
try {
return $this->$wrappedMethodName($args);
} catch (Exception $e) {
return [];
}
}
,任何時候你想調用此的try-catch包裝,只是追加「安全的」,你想包裝的方法名。全部代碼+例如:
class TestParent {
public function __call($method, $args)
{
// If method call ends with 'Safe'
$isSafeMethod = substr($method, -strlen('Safe')) === 'Safe';
if (!$isSafeMethod) {
trigger_error('Call to undefined method '.__CLASS__.'::'.$method.'()', E_USER_ERROR);
return null;
}
// Strip 'Safe' suffix from method name
$wrappedMethodName = substr($method, 0, strpos($method, 'Safe'));
try {
return $this->$wrappedMethodName($args);
} catch (Exception $e) {
return [];
}
}
}
class TestChild extends TestParent {
public function throwingMethod()
{
throw new RuntimeException();
}
public function succeedingMethod()
{
return 'Success';
}
}
$child = new TestChild();
// With 'Safe' try-catch invoked in parent
var_dump($child->throwingMethodSafe()); // Empty array
var_dump($child->succeedingMethodSafe()); // 'Success'
// Without 'Safe' try-catch
var_dump($child->throwingMethod()); // throws RuntimeException as expected
Output in 3v4l.org
旁註:請不要趕Exception
類,因爲它是過於籠統,會使以後調試一個人間地獄(「爲什麼這個方法返回一個數組「)
在這種情況下,我會創建一個全局事件,然後監聽它,如果* any *子類中的任何異常已被拋出。簡單地說,每當一個Exception在「任何孩子」被拋出時,你必須發送一個信號給父類。 –
真@Vural Acar,但如果它是一個被覆蓋的方法。它是一樣的嗎?只是好奇 – Akintunde007
@Akintunde,正確,這就是爲什麼我說*任何*孩子,因爲你必須繼承才能覆蓋。所以你將有一個子類,它引發一個異常,你必須在該方法中發出一個信號。 –