Bug #75190 [NEW]: Impossible to close PDO connection when extending PdoStatement using $this
From: ian at oshaughnessy dot cc
Operating system: Debian 9
PHP version: 7.0.23
Package: PDO related
Bug Type: Bug
Bug description:Impossible to close PDO connection when extending PdoStatement using $this
Description:
------------
The only way to close a PDO connection is to set the resource variable
to null (thereby destroying the PDO object); however, when extending the
PdoStatement class, it's required that $this is passed as an option to
the PDO object for Pdo::ATTR_STATEMENT_CLASS.
In doing this, a reference to the PDO object is created, making it
impossible to destroy the PDO object since a lingering reference
continues to exist after $pdo is set to null.
The only way to fix this issue is to use Pdo::setAttribute() after the
PDO parent constructor has been run.
Test script:
---------------
<?php
echo "Test 1 (PdoTestBroken)\n";
echo "new PdoTestBroken\n";
$pdo1 = new PdoTestBroken($dsn, $username, $password, []);
echo "Destroy \$pdo\n";
$pdo1 = null;
echo "----------------------------\n\n";
echo "Test 2 (PdoTestWorks)\n";
echo "new PdoTestWorks\n";
$pdo2 = new PdoTestWorks($dsn, $username, $password, []);
echo "Destroy \$pdo\n";
$pdo2 = null;
echo "----------------------------\n\n";
class PdoTestBroken extends Pdo {
public function __construct($dsn, $username, $passwd, $options) {
echo "PdoTest1::__construct()\n";
$options[self::ATTR_STATEMENT_CLASS] = [ 'PdoStatementTest', [ $this,
] ];
parent::__construct($dsn, $username, $passwd, $options);
}
public function __destruct() {
echo "PdoTest1::__destruct()\n";
}
}
class PdoTestWorks extends Pdo {
public function __construct($dsn, $username, $passwd, $options) {
echo "PdoTest2::__construct()\n";
$self = parent::__construct($dsn, $username, $passwd, $options);
$this->setAttribute(PDO::ATTR_STATEMENT_CLASS, [ 'PdoStatementTest', [
$self, ] ]);
}
public function __destruct() {
echo "PdoTest2::__destruct()\n";
}
}
class PdoStatementTest extends PdoStatement {
}
Expected result:
----------------
The connection to close.
Actual result:
--------------
The connection does not close due to hidden reference variable.
Script Results:
Test 1 (PdoTestBroken)
new PdoTestBroken
PdoTest1::__construct()
Destroy $pdo
----------------------------
Test 2 (PdoTestWorks)
new PdoTestWorks
PdoTest2::__construct()
Destroy $pdo
PdoTest2::__destruct()
----------------------------
PdoTest1::__destruct()
--
Edit bug report at https://bugs.php.net/bug.php?id=75190&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=75190&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=75190&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=75190&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=75190&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=75190&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=75190&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=75190&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=75190&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=75190&r=support
Expected behavior: https://bugs.php.net/fix.php?id=75190&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=75190&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=75190&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=75190&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75190&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=75190&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=75190&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=75190&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=75190&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=75190&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=75190&r=mysqlcfg
Thread (6 messages)
- ian at oshaughnessy dot cc