Edit report at http://bugs.php.net/bug.php?id=52053&edit=1
ID: 52053 Updated by: fel...@php.net Reported by: zabruska at tugamail dot pt Summary: PDO constructor returns null instead of throwing an exception -Status: Open +Status: Closed Type: Bug Package: PDO related Operating System: Ubuntu 9.10 PHP Version: 5.3.2 -Assigned To: +Assigned To: felipe New Comment: This bug has been fixed in SVN. Snapshots of the sources are packaged every three hours; this change will be in the next snapshot. You can grab the snapshot at http://snaps.php.net/. Thank you for the report, and for helping us make PHP better. Closing the Bug #49320 as well. Previous Comments: ------------------------------------------------------------------------ [2010-06-11 19:36:35] zabruska at tugamail dot pt Description: ------------ Calling 'new PDO(...)' returns null instead of raising an exception. This behaviour was found, while trying to create a PDO object using an sqlite database. The database was created using the command 'sqlite3' from the console. Attempting to open it from PHP using an 'sqlite2' dsn did not raised any exception, but returned null from the object constructor. I do not know if this happens with other PDO drivers, but in any case constructors SHOULD throw an Exception on error, and not NULL. A similar bug has been reported (http://bugs.php.net/bug.php?id=49320) but since there is at least another case (http://bugs.php.net/bug.php?id=52042) in which a constructor returns NULL when it should be throwing an Exception, I decided to report this anyway. I'm not sure if this goes beyond PDO, and it's a PHP feature that I've never experienced before, but this type of code makes no sense at all, $o = new SomeObject(); if($o === null) { // handle error ... } else { $o->doSomething(); } This is what is expected from OOP: try { $o = new SomeObject(); $o->doSomething(); } catch(Exception $x) { // handle error ... } Test script: --------------- >From the console: $ sqlite3 sqlite.db sqlite> CREATE TABLE T(K VARCHAR(32)); sqlite> .q >From a PHP script: $pdo = new PDO('sqlite2:sqlite.db'); echo 'PDO is ', gettype($pdo), PHP_EOL; Expected result: ---------------- PDO is object Actual result: -------------- PDO is NULL ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=52053&edit=1