Bug #44337 [Com]: PDO::FETCH_CLASS and visibility private (private constructor, private property)
Edit report at https://bugs.php.net/bug.php?id=44337&edit=1
ID: 44337
Comment by: Stephen dot Reay at me dot com
Reported by: uwendel at mysql dot com
Summary: PDO::FETCH_CLASS and visibility private (private
constructor, private property)
Status: No Feedback
Type: Bug
Package: PDO related
Operating System: *
PHP Version: 5.2CVS-2008-03-05 (CVS)
Block user comment: N
Private report: N
New Comment:
This bug still exists in PHP 5.3 - what extra information is required for this to
be fixed?
Previous Comments:
------------------------------------------------------------------------
[2009-05-03 01:00:11] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
------------------------------------------------------------------------
[2009-04-25 14:56:12] jani@php.net
Why is this here? You should discuss stuff on the mailing lists, nobody
seems to read the bug reports (who actually know the stuff..).
------------------------------------------------------------------------
[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 https://bugs.php.net/bug.php?id=44337&edit=1
Thread (4 messages)