Skip to content

Extends PDO, sqliteCreateFunction() and magic method __call() #7803

Open
@MioVisman

Description

@MioVisman

Description

The following code:

<?php
declare(strict_types=1);

class DBStatement extends PDOStatement
{
    protected $db;

    protected function __construct(PDO $db)
    {
        $this->db = $db;
    }
}
class db extends PDO {
    public function __construct(string $dsn, string $username = null, string $password = null, array $options = [])
    {
        $options += [
            self::ATTR_DEFAULT_FETCH_MODE => self::FETCH_ASSOC,
            self::ATTR_EMULATE_PREPARES   => false,
            self::ATTR_STRINGIFY_FETCHES  => false,
            self::ATTR_ERRMODE            => self::ERRMODE_EXCEPTION,
            self::ATTR_STATEMENT_CLASS    => [DBStatement::class, [$this]],
        ];
        parent::__construct($dsn, $username, $password, $options);
        $this->sqliteCreateFunction('CONCAT', function (...$args) {return \implode('', $args);});
    }

    public function __call(string $name, array $args) /* : mixed */
    {
        // Here is the loading of a class with rarely used methods that are different in implementation for different databases.
        // In a real project, an exception is displayed: 
        // PHP OTHER_ERROR: Method 'sqliteCreateFunction' not found in DB driver. in ...\app\Core\DB\Sqlite.php:[70]
    }
}

$db = new db('sqlite:datadb', '', '', []);
var_dump($db->query('SELECT CONCAT(\'123\', \'456\', \'789\')')->fetch());
exit;

Resulted in this output:

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 1 no such function: CONCAT in C:\laragon\www\1.php:33 Stack trace: #0 C:\laragon\www\1.php(33): PDO->query('SELECT CONCAT('...') #1 {main} thrown in C:\laragon\www\1.php on line 33

But I expected this output instead:

array(1) { ["CONCAT('123', '456', '789')"]=> string(9) "123456789" } 

It turns out that the call to the sqliteCreateFunction() method is intercepted by __call()?

P.S. In my project, I extended PDO, extended PDOStatement and use _call () to call rare methods to change the structure of the database. Now I got such an incident when using SQLite.

PHP Version

PHP 8.1.1-Win32-vs16-x64, 8.1.0, 8.0.8, 7.4.5, 7.3.33

Operating System

Windows 7 x64

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions