Magically appeared information on json decode

37 Views Asked by At

I am having a really strange issue, but first I will explain the context. Basically I am creating a small wrapper for the facebook API, which uses JSON. Let's dive into the code:

$json_object = array(
     "new" => HttpUtils::makeRequest($new_url),
     "old" => HttpUtils::makeRequest($old_url)
);
echo var_dump(json_decode($json_object['new'], TRUE)); // Echo 1
if (isset($json_object["new"]) && isset($json_object["old"])) {
    $json_object["new"] = $this->handleFeedJSON(json_decode($json_object["new"], TRUE));
    $json_object["old"] = $this->handleFeedJSON(json_decode($json_object["old"], TRUE));
} else {
    return self::ERROR_JSON;
}

Obviously this is in a class, but that context does not matter. The first var_dump gives the following output:

array(2) {
  ["data"]=>
  array(5) {
    [0]=>
    array(4) {
      ["message"]=>
      string(421) ""
      ["story"]=>
      string(60) ""
      ["created_time"]=>
      string(24) ""
      ["id"]=>
      string(32) ""
    }
    [1]=>
    array(3) {
      ["story"]=>
      string(53) ""
      ["created_time"]=>
      string(24) ""
      ["id"]=>
      string(32) ""
    }
    [2]=>
    array(4) {
      ["message"]=>
      string(51) ""
      ["story"]=>
      string(61) ""
      ["created_time"]=>
      string(24) ""
      ["id"]=>
      string(32) ""
    }
    [3]=>
    array(3) {
      ["story"]=>
      string(53) ""
      ["created_time"]=>
      string(24) ""
      ["id"]=>
      string(32) ""
    }
    [4]=>
    array(3) {
      ["message"]=>
      string(466) ""
      ["created_time"]=>
      string(24) ""
      ["id"]=>
      string(32) ""
    }
  }
  ["paging"]=>
  array(2) {
    ["previous"]=>
    string(298) "link_prev"
    ["next"]=>
    string(285) "link_next"
  }
}

This output is emptied because those contents are not really relevant. The actual decoding is put through the handleFeedJSON function, which looks like this:

public function handleFeedJSON($feed_json) {
    echo var_dump($feed_json); // Second var_dump
    $json_object = array(
        "new_uuid" => urlencode($this->UUIDForURL($feed_json["paging"]["next"])),
        "old_uuid" => urlencode($this->UUIDForURL($feed_json["paging"]["previous"])),
        "feed" => array()
    );
    // More json handling
}

However this is the last part of the var_dump in this script:

// stub, same as previous var_dump
 ["paging"]=>
   array(2) {
     ["previous"]=>
     string(298) "prev_link"
     ["next"]=>
     string(285) "next_link"
 }
}
array(1) {
  ["data"]=>
  array(0) {
  }
}
{"new":null,"old":null}

Suddeny objects have been added! Then obviously index errors occur and such, however my code apperantly adds those two objects to the json which screws up my script. Am I being really stupid and am I adding those to the JSON or is something else going on?

By the way I am using this through a Laravel localhost, however I think that should not give me different results.

EDIT the issue seems to be that I am calling the handleFeedJSON function from within the array. When this is outside the array, no errors are encountered.

1

There are 1 best solutions below

2
On

I can't see exactly from your code, but it sure seems like you are having an issue with objects being passed by reference when you weren't expecting it.

Consider this code:

function add_something($passed_var) {
  if (is_array($passed_var)) {
    $passed_var['key2'] = 'value2';
  } else {
    $passed_var->key2 = 'value2';
  }
}

$arr = array('key' => 'value');
$obj = new stdClass();
$obj->key = 'value';

echo '<pre>'.print_r($arr, 1).'<br>';
echo '<pre>'.print_r($obj, 1).'<br>';

add_something($arr);
add_something($obj);

echo '<pre>'.print_r($arr, 1).'<br>';
echo '<pre>'.print_r($obj, 1).'<br>';

Output:

Array
(
  [key] => value
)

stdClass Object
(
  [key] => value
)

Array
(
  [key] => value
)

stdClass Object
(
  [key] => value
  [key2] => value2
)

The final Object output (after calling add_something()) has an unexpected modification outside the scope of the function because

objects are passed by references by default