0

I'm developing front-end code for a web application, and ran into an odd piece of trouble with a custom object. When I request the object and use print_r() I get this (the object is a lot bigger; just cut it to the relevant code):

MemberInfo Object
(
    [meta_data] => Array
    (
        [email_recommendations] => true
        [email_updates] => false
    )
)

To change something in the MemberInfo object, I just update its properties and send it back to the backend with a second function. So for instance, the page is loaded once (which gives us the object shown above), then I send a POST request with changes on a second load of the page. During the second load, I fetch the object above, set one field differently based on the POST with something like $memberInfo->meta_data['email_recommendations'] = 'false'; and then use that version of the object to populate the page after running the update function (which is something like updateMember($memberInfo);). However, once I've changed the object property value print_r() shows me something different:

MemberInfo Object
(
    [meta_data] => {\"email_recommendations\":\"false\",\"email_updates\":\"false\"}

)

I'm sure I'm overlooking something very stupid; does anyone have a good idea of what I should be looking for? I checked, and the backend code isn't passing by reference (the function call is updateMember(MemberInfo $memberInfo);) but I'm a little shaky on my PHP 5 object handling so I'm not sure what might be going wrong.

I don't expect in-depth debugging; I just need to know the general direction I should be looking for what is causing this change in a property that by all rights should be an array.

Thanks in advance!

2
  • very strange... looks like the member has been put through json_encode() Commented Nov 28, 2008 at 22:15
  • Yeah; I think that's how it's being stored in the database, but I can't figure out why the array that I'm modifying would change itself like that (particularly since the function call isn't passing by reference). Commented Nov 28, 2008 at 22:18

1 Answer 1

1

so are you using the object after calling updateMember()? PHP5 objects are passed by reference by default, so if you're calling json_encode() on the meta_data property, it'll exhibit the behaviour you describe.

you may want to post the updateMember() function to confirm, but it sounds like that's what's going on.

ie:

class MemberInfo {
    function __construct()  {
        $this->meta_data = array(
            'email_recommendations' => true,
            'email_updates' => false,
        );
    } 
}

function updateMember($meminfo) {
    $meminfo->meta_data = json_encode($meminfo->meta_data);
    // do stuff
}

$meminfo = new MemberInfo();

updateMember($meminfo);

print_r($meminfo); // you'll see the json encoded value for "meta_data"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Owen! I hadn't known that little tidbit (knew it was going to be some minor thing that I was overlooking or not aware of). I'll see about a workaround, or else decode the thing on my end after I've run the update function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.