2010-04-24 39 views
1

有一種方法可以檢查「IF」如果函數在php中失敗?在PHP函數上檢查和錯誤

Ex。

If (getimagesize($image) returns and error) { 
echo 'Error: function dont work'; 
} 
else 
{ 
// do something 
} 

謝謝!

回答

5

我假設你對getimagesize這樣的函數有特別的興趣,它不會返回任何錯誤代碼,並且對我們很難。但並非不可能。

的文檔getimagesize狀態:

如果訪問的文件名的圖像是不可能的,或者如果它不是一個有效的圖片,getimagesize()將生成E_WARNING電平的誤差。在讀取錯誤時,getimagesize()將生成級別爲E_NOTICE的錯誤。

因此,你需要做兩件事情:在其

  1. 獲得通知的錯誤和行爲在某種程度上
  2. 還沒有顯示錯誤或以其他方式影響你的程序的執行在所有(畢竟,你要與錯誤處理代碼自己服用任何錯誤的護理)

可以實現使用set_error_handler()restore_error_handler()的第一個。您可以通過error-control operator@實現第二個。

因此,代碼必須是這樣的:

// Set our own error handler; we will restore the default one afterwards. 
// Our new error handler need only handle E_WARNING and E_NOTICE, as per 
// the documentation of getimagesize(). 
set_error_handler("my_error_handler", E_WARNING | E_NOTICE); 

// No error has occured yet; it is the responsibility of my_error_handler 
// to set $error_occurred to true if an error occurs. 
$error_occurred = false; 

// Call getimagesize; use operator @ to have errors not be generated 
// However, your error handler WILL STILL BE CALLED, as the documentation 
// for set_error_handler() states. 
$size = @getimagesize(...); 

// At this point, my_error_handler will have run if an error occurred, and 
// $error_occurred will be true. Before doing anything with it, restore the 
// previous error handler 
restore_error_handler(); 

if($error_occurred) { 
    // whatever 
} 
else { 
    // no error; $size holds information we can use 
} 


function my_error_handler($errno, $errstr, $file, $line) { 
    global $error_occurred; 

    // If the code is written as above, then we KNOW that an error 
    // here was caused by getimagesize(). We also know what error it was: 
    switch($errno) { 
     case E_WARNING: // Access to image impossible, or not a valid picture 
     case E_NOTICE: // Read error 
    } 

    // We could also check what $file is and maybe do something based on that, 
    // if this error handler is used from multiple places. However, I would not 
    // recommend that. If you need more functionality, just package all of this 
    // into a class and use the objects of that class to store more state. 

    $error_occurred = true; 
    return true; // Do not let PHP's default error handler handle this after us 
} 

當然,這不是很維護(你有一個全局變量$error_occurred那裏,這不是一個很好的做法)。因此,對於不僅可以正常工作而且設計得很好的解決方案,您可以將所有這些解決方案打包到一個課堂中。該類將定義:

  1. 一種實現錯誤處理程序(在上例中爲my_error_handler)的方法。要將對象方法設置爲錯誤處理程序而不是全局函數,則需要使用合適的第一個參數調用set_error_handler;見the documentation for callback
  2. 一種讓類設置錯誤處理程序,執行您選擇的代碼,保存「執行代碼時發生的錯誤」標誌並恢復錯誤處理程序的方法。該方法可以例如請撥打callback以提供您的呼叫代碼和一組參數,然後使用call_user_func_array執行此操作。如果在執行過程中調用了上面#1中設置的錯誤處理程序,請將其標記爲對象中的變量。您的方法會將返回值call_user_func_array返回給調用代碼。
  3. 調用代碼可用於訪問上述#2結果的方法或變量。

那麼,如果那類叫做ErrorWatcher,調用代碼會是這樣的:

$watcher = new ErrorWatcher; 
$size = $watcher->watch("getimagesize", 
         array(/* params for getimagesize here */)); 

// $size holds your result, if an error did not occur; 
// check for errors and we 're done! 

switch($watcher->report_last_error()) { 
    // error handling logic here 
} 

...這是好的,整齊不亂用全局變量。我希望我解釋得很好,可以讓你自己編寫類ErrorWatcher。 :-)

+0

謝謝,這是最好的答案 – DomingoSL 2010-04-24 02:51:40

0

看看exceptions。儘管如此,你必須把它們放入你的函數中。

編輯:順便說一句,如果你的函數不應該返回一個布爾值,你可以總是有它返回false,如果出現錯誤和檢查,如:

$result = getimagesize($image); 

if ($result === false) 
{ 
    // 
} 
+0

使用邏輯流異常是我書中的代碼異味。 – LiraNuna 2010-04-24 01:01:21

+0

@LiraNuna,看看這個問題:「如果(getimagesize($ image)返回並且出錯)」。 **錯誤**這個詞全說了;不是正常的邏輯流程,所以絕對是例外的地方。 – jeroen 2010-04-24 01:13:29

+0

不,它不是布爾函數。你能幫我一個例子代碼? – DomingoSL 2010-04-24 01:23:49

0

無論條件是「if」語句(括號內的內容)將返回「true」或「false」。 如果條件返回「true」,則將執行第一條語句,否則將執行第二條語句。

你可以把這個,error_reporting(E_ALL);在你的腳本的最頂端,在你打開php標籤之後,看看你得到了什麼錯誤。

希望它有幫助。

也許是這樣的:

<?php 
error_reporting(E_ALL); 
more stuff here ... 
if(your condition here){ 
echo "condition is true, do something"; 
}else{ 
echo "condition is not true, do something else"; 
} 

,如果你的「如果」或「其他」裏面運行功能或語句失敗,至少你知道在它的基礎上其結果之一,而使用error_reporting可能會告訴你爲什麼失敗。

+0

你能告訴我更多的代碼示例嗎? – DomingoSL 2010-04-24 01:04:46

+0

見上面,我編輯了我的答案。 – jnkrois 2010-04-24 01:32:18