Check if variable Is PDO Object?

mysql, pdo, php

Solution

instanceof is used to determine whether a PHP variable is an instantiated object of a certain class:

if($var instanceof PDO) {
   // your code
}

Problem

I have a universal function that I use for processing SQL. I am getting this error (just a few times a day, not frequently). ``` PHP Catchable fatal error: Object of class PDO could not be converted to string in... ``` Basically, an array of values is passed for a function that I am using, and I must have slipped up in my code and placed a PDO object in that array. I need to make an array_filter function that checks if the variable is a PDO object. How do I do a simple if statement for this? ``` if($var == PDO) ``` Edit: Thanks for the great answers! In case anyone is interested, here is how I solved the problem. I was able to find where the invalid input was coming from. ``` $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("admin@email.com","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); } ```

Original source