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
Have you experienced this issue?
Rate the importance of this bug to you:

 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2019 The PHP Group
All rights reserved.
Last updated: Sun Aug 25 04:01:26 2019 UTC