How to Check for a Specific Type of Object in PHP

后端 未结 4 1386
Happy的楠姐
Happy的楠姐 2020-12-01 06:09

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:

4条回答
  •  清歌不尽
    2020-12-01 06:35

    use

     bool is_a ( object $object , string $class_name )
    

    This will work for child classes too.

    see http://php.net/is-a

    EDIT: Or you could use type hinting:

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

提交回复
热议问题