How to Check for a Specific Type of Object in PHP?

0 votes

I have a method which accepts a PDO object as an argument, to allow the user to use an existing connection rather then the method to open a new one, and save resources:

public static function databaseConnect($pdo = null) {

I am aware of is_object() to check if the argument is an object, but I want to check if $pdo is a PDO object, and not just an object.

Because the user can easily enter (by mistake?) a different kind of object, a mysqli or such, and the entire script will break apart.

How can I check a variable for a specific type of object?

Oct 29 in Laravel by kartik
• 36,040 points
25 views

1 answer to this question.

0 votes

Hello @kartik,

Use:

 bool is_a ( object $object , string $class_name )

This will work for child classes too.

Or you could use type hinting:

public static function databaseConnect(PDO $pdo = null) {...

Hope it helps!!

answered Oct 29 by Niroj
• 78,520 points

Related Questions In Laravel

0 votes
1 answer

How to set port for php artisan.php serve in Laravel?

Hii @kartik, When we use the php artisan serve ...READ MORE

answered Mar 30 in Laravel by Niroj
• 78,520 points
1,899 views
+1 vote
1 answer

How to check if a record already exists in a laravel?

Hey, As a newbie it is most common ...READ MORE

answered Mar 31 in Laravel by Niroj
• 78,520 points
6,234 views
0 votes
1 answer

How to get a list of registered route paths in Laravel?

Hello, Route::getRoutes() returns a RouteCollection. On each element, you can ...READ MORE

answered Mar 31 in Laravel by Niroj
• 78,520 points
372 views
0 votes
1 answer

How to break a for each loop in laravel blade view?

Hello @kartik, By default, blade doesn't have @break and @continue which are ...READ MORE

answered Apr 6 in Laravel by Niroj
• 78,520 points
3,146 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27 in Java-Script by Niroj
• 78,520 points
455 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5 in Java-Script by Niroj
• 78,520 points
706 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8 in PHP by Niroj
• 78,520 points
92 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8 in Node-js by Niroj
• 78,520 points
87 views
0 votes
1 answer

.How to turn off CSRF protection for a particular route in Laravel?

Hey, We can add that particular URL or ...READ MORE

answered Mar 24 in Laravel by Niroj
• 78,520 points
1,184 views
+2 votes
1 answer

How to add a new column to existing table of laravel in a migration?

Hii @kartik, To create a migration, you may ...READ MORE

answered Mar 30 in Laravel by Niroj
• 78,520 points
16,738 views