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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: oleg dot nucer at gmail dot com
New email:
PHP Version: OS:

 

 [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-2020 The PHP Group
All rights reserved.
Last updated: Mon Jul 13 09:01:23 2020 UTC