Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6811747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:21:53+00:00 2026-05-26T20:21:53+00:00

I have a method which accepts a PDO object as an argument, to allow

  • 0

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.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:21:54+00:00Added an answer on May 26, 2026 at 8:21 pm

    You can use instanceof:

    if ($pdo instanceof PDO) {
        // it's PDO
    }
    

    Be aware though, you can’t negate like !instanceof, so you’d instead do:

    if (!($pdo instanceof PDO)) {
        // it's not PDO
    }
    

    Also, looking over your question, you can use object type-hinting, which helps enforce requirements, as well as simplify your check logic:

    function connect(PDO $pdo = null)
    {
        if (null !== $pdo) {
            // it's PDO since it can only be
            // NULL or a PDO object (or a sub-type of PDO)
        }
    }
    
    connect(new SomeClass()); // fatal error, if SomeClass doesn't extend PDO
    

    Typed arguments can be required or optional:

    // required, only PDO (and sub-types) are valid
    function connect(PDO $pdo) { }
    
    // optional, only PDO (and sub-types) and 
    // NULL (can be omitted) are valid
    function connect(PDO $pdo = null) { }
    

    Untyped arguments allow for flexibility through explicit conditions:

    // accepts any argument, checks for PDO in body
    function connect($pdo)
    {
        if ($pdo instanceof PDO) {
            // ...
        }
    }
    
    // accepts any argument, checks for non-PDO in body
    function connect($pdo)
    {
        if (!($pdo instanceof PDO)) {
            // ...
        }
    }
    
    // accepts any argument, checks for method existance
    function connect($pdo)
    {
        if (method_exists($pdo, 'query')) {
            // ...
        }
    }
    

    As for the latter (using method_exists), I’m a bit mixed in my opinion. People coming from Ruby would find it familiar to respond_to?, for better or for worse. I’d personally write an interface and perform a normal type-hint against that:

    interface QueryableInterface
    { 
        function query();
    }
    
    class MyPDO extends PDO implements QueryableInterface { }
    
    function connect(QueryableInterface $queryable) { }
    

    However, that’s not always feasible; in this example, PDO objects are not valid parameters as the base type doesn’t implement QueryableInterface.

    It’s also worth mentioning that values have types, not variables, in PHP. This is important because null will fail an instanceof check.

    $object = new Object();
    $object = null;
    if ($object instanceof Object) {
        // never run because $object is simply null
    }
    

    The value loses it’s type when it becomes null, a lack of type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to use a method which accepts double[,], but I only have a
I have a C# method which accepts a Predicate<Foo> and returns a list of
I have a method in my business logic layer that accepts a stream, which
I have a method, which will accept a parameter of a JQuery Object and
I have a method which takes params object[] such as: void Foo(params object[] items)
I have a method which constructs an object, calls an Execute method, and frees
I have a method which never returns a null object. I want to make
Assuming I have a method which accepts an IList or similar thing as a
I want to build a method which accepts a string param, and an object
I have a method which accepts a filename as a parameter, all filenames should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.