php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67710 PDOStatement::fetch not work for parent class private properties
Submitted: 2014-07-29 18:47 UTC Modified: 2014-07-29 18:57 UTC
From: oleg dot nucer at gmail dot com Assigned:
Status: Not a bug Package: PDO Core
PHP Version: 5.6.0RC2 OS: Windows 8
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
10 - 6 = ?
Subscribe to this entry?

 
 [2014-07-29 18:47 UTC] oleg dot nucer at gmail dot com
Description:
------------
PDOStatement::fetch with PDO::FETCH_CLASS works fine, until I tried to use it with parent class private properties... See the test script.

Test script:
---------------
class Animal
{
	private $color;
	public function getColor()
	{
		return $this->color;
	}
}
class Cat extends Animal
{
}

$statement->setFetchMode(PDO::FETCH_CLASS, "Cat" );
$someCat = $statement->fetch();

echo $someCat->getColor();	//empty
print_r( $someCat );
/*
now have strange output like:
[color:Animal:private] => 
[color] => grey
*/

Expected result:
----------------
All private properties of parent class have to be set same way as public and protected ones.

Actual result:
--------------
Private property of parent class still empty :(

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-07-29 18:57 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-07-29 18:57 UTC] requinix@php.net
It's private. That means the child class does not get that property. As far as PDO is concerned, $someCat->color does not (did not) exist.
Make $color protected so Cat inherits it and so PDO doesn't have to make a brand new property.

By "strange output" I assume you mean the "color:Animal:private" part? PHP is digging deep into the class to look up information; it's not that Cat somehow has Animal's private property $color but that PHP went into Cat's class inheritance hierarchy and discovered $color on the parent class. The "property:class:accessibility" naming scheme is so you know that $color is not part of Cat, but PHP still wants to include the value there for you because it could be useful for debugging.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 19:01:31 2024 UTC