天天看點

php判斷pdo是否打開,php – 檢查變量是否是PDO對象?

我有一個用于處理SQL的通用函數.我收到這個錯誤(一天隻有幾次,不經常).

PHP Catchable fatal error: Object of class PDO could not be converted to string in...

基本上,為我正在使用的函數傳遞一個值數組,并且我必須在我的代碼中滑落并在該數組中放置一個PDO對象.

我需要建立一個array_filter函數來檢查變量是否是PDO對象.我如何為此做一個簡單的if語句?

if($var == PDO)

編輯:謝謝你的答案!如果有人有興趣,這就是我解決問題的方法.我能夠找到無效輸入的來源.

$before=$original_array;

$after = array_filter($before, "find_error");

if(count($before)!=count($after)){

$error=print_r(debug_backtrace(false),true);

$arr=print_r($before,true);

send_message("[email protected]","Error Report",$arr.$error);

//send_message is a simple function for sending emails. You could also write information to a txt file, etc.

}

function find_error($var){

return !($var instanceof PDO);

}

解決方法:

instanceof用于确定PHP變量是否是某個類的執行個體化對象:

if($var instanceof PDO) {

// your code

}

标簽:php,mysql,pdo

來源: https://codeday.me/bug/20190713/1450306.html