Bug #67710 [Opn->Nab]: PDOStatement::fetch not work for parent class private properties
| From: | requinix@php.net | Date: | Tue, 29 Jul 2014 18:57:18 +0000 |
| Subject: | Bug #67710 [Opn->Nab]: PDOStatement::fetch not work for parent class private properties | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-186867@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67710&edit=1
ID: 67710
Updated by: requinix@php.net
Reported by: oleg dot nucer at gmail dot com
Summary: PDOStatement::fetch not work for parent class
private properties
-Status: Open
+Status: Not a bug
Type: Bug
Package: PDO Core
Operating System: Windows 8
PHP Version: 5.6.0RC2
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2014-07-29 18:47:53] 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 :(
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67710&edit=1