I have a function defined like this in a class that’s widely used:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>/**
* Executes a PDO statement and, if any error, logs the sql (with data merged) along with the error message
* @param myPDOStatement|PDOStatement $stmt the prepared PDO statement
* @param array $data the data to be used with the statement
* @param int $flags supported flags: OPT_FAIL_ON_ERROR
* @return array | bool if succssful, result of stmt->execute(); Othewise, array: element 'error' will be true, 'errorInfo' will be string, 'errorInfo()' will be PDO errorInfo()
*/
public functionexecutePDOStatement(myPDOStatement|PDOStatement $stmt, array $data = [], int $flags = 0){
</code>
<code>/**
* Executes a PDO statement and, if any error, logs the sql (with data merged) along with the error message
* @param myPDOStatement|PDOStatement $stmt the prepared PDO statement
* @param array $data the data to be used with the statement
* @param int $flags supported flags: OPT_FAIL_ON_ERROR
* @return array | bool if succssful, result of stmt->execute(); Othewise, array: element 'error' will be true, 'errorInfo' will be string, 'errorInfo()' will be PDO errorInfo()
*/
public function executePDOStatement(myPDOStatement|PDOStatement $stmt, array $data = [], int $flags = 0) {
</code>
/**
* Executes a PDO statement and, if any error, logs the sql (with data merged) along with the error message
* @param myPDOStatement|PDOStatement $stmt the prepared PDO statement
* @param array $data the data to be used with the statement
* @param int $flags supported flags: OPT_FAIL_ON_ERROR
* @return array | bool if succssful, result of stmt->execute(); Othewise, array: element 'error' will be true, 'errorInfo' will be string, 'errorInfo()' will be PDO errorInfo()
*/
public function executePDOStatement(myPDOStatement|PDOStatement $stmt, array $data = [], int $flags = 0) {
This function will accept either of two classes passed to it. The one of interest here, is the PDOStatement class. When this function is called using a PDOStatement object, the call fails with this error:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[18-Jul-202410:37:01 America/New_York] PHP Fatal error: Uncaught TypeError: loveBaseFunctions::executePDOStatement(): Argument #1 ($stmt) must be of type myPDOStatement|DoctrineDBALDriverPDOStatement, PDOStatement given, called in /www/htdocs/functions/loveBaseFunctions.php on line 4363 and defined in /www/htdocs/functions/loveBaseFunctions.php:2194
thrown in /www/htdocs/functions/loveBaseFunctions.php on line 2194
</code>
<code>[18-Jul-2024 10:37:01 America/New_York] PHP Fatal error: Uncaught TypeError: loveBaseFunctions::executePDOStatement(): Argument #1 ($stmt) must be of type myPDOStatement|DoctrineDBALDriverPDOStatement, PDOStatement given, called in /www/htdocs/functions/loveBaseFunctions.php on line 4363 and defined in /www/htdocs/functions/loveBaseFunctions.php:2194
Stack trace:
#0 /www/htdocs/functions/loveBaseFunctions.php(4363): loveBaseFunctions->executePDOStatement()
#1 /www/htdocs/gdrive/catchupSongBook-v1(44): loveBaseFunctions->checkApplicationControl()
#2 {main}
thrown in /www/htdocs/functions/loveBaseFunctions.php on line 2194
</code>
[18-Jul-2024 10:37:01 America/New_York] PHP Fatal error: Uncaught TypeError: loveBaseFunctions::executePDOStatement(): Argument #1 ($stmt) must be of type myPDOStatement|DoctrineDBALDriverPDOStatement, PDOStatement given, called in /www/htdocs/functions/loveBaseFunctions.php on line 4363 and defined in /www/htdocs/functions/loveBaseFunctions.php:2194
Stack trace:
#0 /www/htdocs/functions/loveBaseFunctions.php(4363): loveBaseFunctions->executePDOStatement()
#1 /www/htdocs/gdrive/catchupSongBook-v1(44): loveBaseFunctions->checkApplicationControl()
#2 {main}
thrown in /www/htdocs/functions/loveBaseFunctions.php on line 2194
It would be funny if it wasn’t so sad that the error message says that the type of object passed (PDOStatement) is exactly the type of object specified in the docblock and in the function definition. Short of dropping to the less-desirable type of “object” here, what can be done to ensure that PDOStatement does not get converted to DoctrineDBALDriverWhateverItIs ??? Same thing happens with PDO object, by the way.
This is on a PHP 8.1 system, but also occurs on lower versions (to 7.x at least)