PDO using PDO::FETCH_PROPS_LATE and __construct() call?

后端 未结 1 1400
不知归路
不知归路 2020-12-10 00:16

I\'m trying to create a new instance of Setting object calling __construct() method with PHP PDO and constrain PDO::FETCH_

相关标签:
1条回答
  • 2020-12-10 00:35

    You have a non defaulted parameter $key in your constructor:

    public function __construct($key, $value = null, $displayable = 1)
    

    So, when you are doing this:

    $settings = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,'Setting');
    

    Error: warning: Missing argument 1 for Setting::__construct() in pdo.php is thrown only for parameter $key because it is not defaulted.

    The correct use of fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,... is like this:

    $variable = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,
                               'classname', 
                                <array of parameter names(in order) used in constructor>);
    

    So, in your case:

    $variable = $stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,
                                'Setting', 
                                 array('key', 'value', 'displayable');
    
    0 讨论(0)
提交回复
热议问题