From: uwendel at mysql dot com Operating system: PHP version: 5.2CVS-2008-03-05 (CVS) PHP Bug Type: PDO related Bug description: PDO::FETCH_CLASS and visibility private (private constructor, private property)
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 bug report at http://bugs.php.net/?id=44337&edit=1 -- Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=44337&r=trysnapshot52 Try a CVS snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=44337&r=trysnapshot53 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=44337&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=44337&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=44337&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=44337&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=44337&r=needscript Try newer version: http://bugs.php.net/fix.php?id=44337&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=44337&r=support Expected behavior: http://bugs.php.net/fix.php?id=44337&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=44337&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=44337&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=44337&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=44337&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=44337&r=dst IIS Stability: http://bugs.php.net/fix.php?id=44337&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=44337&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=44337&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=44337&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=44337&r=mysqlcfg