php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49096 json_encode has side effect
Submitted: 2009-07-29 09:28 UTC Modified: 2009-08-06 01:00 UTC
From: xiezhenye at gmail dot com Assigned:
Status: No Feedback Package: JSON related
PHP Version: 5.2.10 OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: xiezhenye at gmail dot com
New email:
PHP Version: OS:

 

 [2009-07-29 09:28 UTC] xiezhenye at gmail dot com
Description:
------------
json_encode has side effect and may affect array propterty and cause RECURSION.


Reproduce code:
---------------
class Test {
    protected $data;
    function __construct($data) {
        $this->data = $data;
    }
}
$t = new Test(array('a'=>1, 'b'=>2));
print_r($t);
json_encode($t);
print_r($t);


Expected result:
----------------
Test Object
(
    [data:protected] => Array
        (
            [a] => 1
            [b] => 2
        )

)
Test Object
(
    [data:protected] => Array
        (
            [a] => 1
            [b] => 2
        )

)

Actual result:
--------------
Test Object
(
    [data:protected] => Array
        (
            [a] => 1
            [b] => 2
        )

)
Test Object
(
    [data:protected] => Array
 *RECURSION*
)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-29 09:33 UTC] xiezhenye at gmail dot com
addition:
it will apears when the properties are protected or private.
 [2009-07-29 11:30 UTC] scottmac@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.

Output from what you've given is:

Test Object
(
    [data:protected] => Array
        (
            [a] => 1
            [b] => 2
        )

)
Test Object
(
    [data:protected] => Array
        (
            [a] => 1
            [b] => 2
        )

)

 [2009-08-06 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC