php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53586 parent::__construct destroys object if exception is thrown
Submitted: 2010-12-21 10:03 UTC Modified: 2010-12-21 18:39 UTC
Votes:4
Avg. Score:4.2 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:2 (66.7%)
From: peters at yandex dot ru Assigned:
Status: Not a bug Package: PDO related
PHP Version: 5.3.4 OS: Linux
Private report: No CVE-ID: None
 [2010-12-21 10:03 UTC] peters at yandex dot ru
Description:
------------
When calling parent::__construct() in any(!) method of PDO successor class and exception is thrown in parent::__construct() successor object is destroyed regardless of the fact that exception is properly caught.

Test script:
---------------
class B extends PDO
{
	public function __construct()
	{
		try
		{
			parent::__construct('');
		}
		catch (Exception $e)
		{
		}
		var_dump($this);
	}
}

new B('');

Expected result:
----------------
object(B)#45 (0) { } 

Actual result:
--------------
NULL

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-21 18:39 UTC] cataphract@php.net
-Status: Open +Status: Bogus
 [2010-12-21 18:39 UTC] cataphract@php.net
This is a proper, safe way to handle the exception. The exception in a constructor means the object is likely not in a valid state after it. PHP itself doesn't require parent constructors to be called and, if they're called and there's an exception, it allows catching the exception. In my opinion this a bad choice, but for internal objects it's much worse because the consequences of an internal object in an invalid state are segfaults and other problems.

Closing as bogus.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 28 08:01:29 2024 UTC