#40465 [Bgs->Opn]: var_dump() printing details of private and protected object properties
| From: | derick@php.net | Date: | Tue, 13 Feb 2007 22:18:14 +0000 |
| Subject: | #40465 [Bgs->Opn]: var_dump() printing details of private and protected object properties | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-108829@lists.php.net to get a copy of this message | ||
ID: 40465
Updated by: derick@php.net
Reported By: wharmby at uk dot ibm dot com
-Status: Bogus
+Status: Open
Bug Type: Variables related
Operating System: Windows XP
PHP Version: 5CVS-2007-02-13 (CVS)
New Comment:
Ilia, learn to read ;-)
Previous Comments:
------------------------------------------------------------------------
[2007-02-13 19:53:14] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
.
------------------------------------------------------------------------
[2007-02-13 19:08:18] wharmby at uk dot ibm dot com
btw.. Given that the code behaves in the intended manner removing the
spurious code in the HEAD (PHP 6) stream only would probably suffice
here.
------------------------------------------------------------------------
[2007-02-13 19:05:41] wharmby at uk dot ibm dot com
Thanks Derick. I had assumed as much from comments in previous defects
on var_dump(). var_dump() is a debug aid
rather than something a PHP application should be using
and if it did NOT print out details regarding private and protected
fields that would be a pain.
Which means that the 2nd patch attached is the one which is needed here
to remove the spurious and bogus code.
------------------------------------------------------------------------
[2007-02-13 18:44:09] derick@php.net
Just FYI: var_dump should show the private and protected properties.
------------------------------------------------------------------------
[2007-02-13 16:21:53] wharmby at uk dot ibm dot com
Description:
------------
I have come across the following behaviour while reviewing
the var_dump code which is either a bug or the code needs
tidying up. The output certainly does not tally with the
behaviour suggested by the code.
My reading of the code in php_array_element_dump() is that
if an Object with private or protected properties is cast to
an array and then dumped using var_dump() then no private or
protected fields should be dumped (previous defects suggest otherwise
but the code suggests they should not be printed)
The code in php_array_element_dump() is as follows:
level = va_arg(args, int);
if (hash_key->nKeyLength==0) { /* numeric key */
php_printf("%*c[%ld]=>\n", level + 1, ' ', hash_key->h);
} else { /* string key */
if (va_arg(args, int) && hash_key->arKey[0] == '\0') {
/* XXX: perhaps when we are inside the class we
* should permit access to private & protected
* values
*/
return 0;
}
php_printf("%*c[\"", level + 1, ' ');
PHPWRITE(hash_key->arKey, hash_key->nKeyLength - 1);
php_printf("\"]=>\n");
}
However, because of a logic error in php_var_dump()the
2nd argument does not get passed to php_array_element_dump()
and so we print out the details of private and protected
fields.
Assuming the intention is NOT to print out private or protected fileds
the patch to correct the var_dump_code
is as follows:
http://www.pastebin.ca/353743
However, previous defects have been raised on this subject
and closed as "Expected Behaviour". If it is the case that all fields
should be printed by var_dump to aid debug then
the code in php_array_element_dump() and php_var_dump() should be
cleaned up as in the following patch:
http://www.pastebin.ca/353787
Both patches built against CVS code as of 15:30 GMT on 13th Feb 2007.
Reproduce code:
---------------
<?php
class foo
{
public $a = 10;
private $b = 20;
public $c = 40;
protected $d = 50;
public $e = 60;
}
$obj = new foo();
var_dump($obj);
$fooarr = (array)$obj;
var_dump($fooarr);
?>
Expected result:
----------------
object(foo)#1 (5) {
["a"]=>
int(10)
["b:private"]=>
int(20)
["c"]=>
int(40)
["d:protected"]=>
int(50)
["e"]=>
int(60)
}
array(5) {
["a"]=>
int(10)
["c"]=>
int(40)
["e"]=>
int(60)
}
Actual result:
--------------
object(foo)#1 (5) {
["a"]=>
int(10)
["b:private"]=>
int(20)
["c"]=>
int(40)
["d:protected"]=>
int(50)
["e"]=>
int(60)
}
array(5) {
["a"]=>
int(10)
[" foo b"]=>
int(20)
["c"]=>
int(40)
[" * d"]=>
int(50)
["e"]=>
int(60)
}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=40465&edit=1