2017-08-29 28 views
5

拋出的異常假設我有一個父類:通用的方法來捕獲和處理子類中

class Parent { 

    //... 

} 

和子類與方法:

class Child extends Parent { 

    public function foo() { 
     // process and return data 
    } 

    public function bar() { 
     // process and return data 
    } 

    // way more methods... 

} 

那麼有沒有在PHP中一個通用的方法處理子方法中引發的任何異常?例如,父類中的處理程序?或者我是否需要將所有方法體包裝在單獨的try-catch塊中?

我想實現的是,如果有任何子方法拋出任何異常,則返回空array()

+1

在這種情況下,我會創建一個全局事件,然後監聽它,如果* any *子類中的任何異常已被拋出。簡單地說,每當一個Exception在「任何孩子」被拋出時,你必須發送一個信號給父類。 –

+0

真@Vural Acar,但如果它是一個被覆蓋的方法。它是一樣的嗎?只是好奇 – Akintunde007

+1

@Akintunde,正確,這就是爲什麼我說*任何*孩子,因爲你必須繼承才能覆蓋。所以你將有一個子類,它引發一個異常,你必須在該方法中發出一個信號。 –

回答

-1

根據我的個人經驗,如果發生異常,請創建自定義異常處理程序並返回空數組。 這些鏈接將幫助您瞭解例外PHP處理: https://www.w3schools.com/php/php_exception.asp http://php.net/manual/en/language.exceptions.php

+1

我很肯定這不是OP所要求的。 – Naruto

+0

OP正在談論面向對象的方式。這是關閉的 – Akintunde007

+0

OP知道如何處理異常,他問,是否有可能在父類中拋出異常,並拋出子類。 –

1

是的,這是可能的。那麼,父母無法知道全部是可能的子方法,但它可以通過知道知道什麼時候通過實現__callmagic 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類,因爲它是過於籠統,會使以後調試一個人間地獄(「爲什麼這個方法返回一個數組「)

相關問題