php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52053 PDO constructor returns null instead of throwing an exception
Submitted: 2010-06-11 19:36 UTC Modified: 2010-06-13 00:05 UTC
From: zabruska at tugamail dot pt Assigned: felipe (profile)
Status: Closed Package: PDO related
PHP Version: 5.3.2 OS: Ubuntu 9.10
Private report: No CVE-ID: None
 [2010-06-11 19:36 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-13 00:05 UTC] felipe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: felipe
 [2010-06-13 00:05 UTC] felipe@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 14:01:30 2024 UTC