ID: 44337
Updated by: [email protected]
Reported By: uwendel at mysql dot com
-Status: Open
+Status: Feedback
Bug Type: PDO related
Operating System: *
PHP Version: 5.2CVS-2008-03-05 (CVS)
New Comment:
Why is this here? You should discuss stuff on the mailing lists, nobody
seems to read the bug reports (who actually know the stuff..).
Previous Comments:
------------------------------------------------------------------------
[2008-03-05 15:29:34] uwendel at mysql dot com
Description:
------------
Please clearify if any of the following is a bug or a feature.
1) PDO::FETCH_CLASS and private constructor
PDO ignores the visibility "private" of a constructor and creates
objects of the requested type when using PDO::FETCH_CLASS and
PDOStatement->fetch().
Create a class with a private constructor and try to create an object
of the class from somewhere outside of the class. PHP will print a fatal
error as expected.
class private_constructor {
private function __construct()
}
$obj = new private_constructor() --> Fatal error (OK)
Use PDOStatement->setFetchMode() to make PDO return objects of the type
"private_constructor" when fetching results. setFetchMode() will return
true and PDOStatement->fetch() will return objects of the type
"private_constructor". In other words: PDO will ignore the visibility of
the constructor and behave as if PDO would be a part of the class
"private_constructor"
2) PDO::FETCH_CLASS and private properties
Something similar happens if you make PDO instantiate a class with
private properties which have the same name as column in the result set.
PDO does not bother about private and fills the appropriate properties
with values from the result set.
Reproduce code:
---------------
---------------- 1 - private constructor -------------
php -r '$db = new PDO("sqlite:/tmp/foo"); $db->exec("DROP TABLE test");
$db->exec("CREATE TABLE test (id INT)"); $db->exec("INSERT INTO test(id)
VALUES (1)"); $stmt = $db->prepare("SELECT id FROM test"); class
private_constructor { public static $calls = 0; private function
__construct() { printf("private_constructor: %d\n", self::$calls++); }}
$stmt->setFetchMode(PDO::FETCH_CLASS, 'private_constructor');
$stmt->execute(); var_dump($stmt->fetch());'
private_constructor: 0
object(private_constructor)#3 (1) {
["id"]=>
string(1) "1"
}
--------------------- 2 - private properties -------------------
php -r '$db = new PDO("sqlite:/tmp/foo"); $db->exec("DROP TABLE test");
$db->exec("CREATE TABLE test (id INT)"); $db->exec("INSERT INTO test(id)
VALUES (1)"); $stmt = $db->prepare("SELECT id FROM test"); class
private_properties { public static $calls = 0; private $id; public
function __construct() { printf("private_properties: %d\n",
self::$calls++); }} var_dump($stmt->setFetchMode(PDO::FETCH_CLASS,
'private_properties')); $stmt->execute(); var_dump($stmt->fetch());'
bool(true)
private_properties: 0
object(private_properties)#3 (1) {
["id":"private_properties":private]=>
string(1) "1"
}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=44337&edit=1