简体   繁体   English

PHP数组中的对象转换为字符串(据我所知)

[英]PHP array in object converting to string (so far as I can tell)

I'm developing front-end code for a web application, and ran into an odd piece of trouble with a custom object. 我正在为Web应用程序开发前端代码,但是使用自定义对象遇到了麻烦。 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): 当我请求对象并使用print_r()我得到了这个(对象大得多;只需将其剪切为相关代码即可):

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. 要更改MemberInfo对象中的某些内容,我只需更新其属性,然后使用第二个函数将其发送回后端。 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. 因此,例如,页面被加载了一次(这给了我们上面显示的对象),然后我发送了POST请求,并在页面第二次加载时进行了更改。 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'; 在第二次加载期间,我获取了上面的对象,并根据POST设置了一个不同的字段,例如$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); ). 然后在运行更新功能(类似于updateMember($memberInfo); )后使用该对象的版本填充页面。 However, once I've changed the object property value print_r() shows me something different: 但是,一旦更改了对象属性值print_r()我就会看到不同的东西:

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. 我检查了一下,后端代码没有通过引用传递(函数调用是updateMember(MemberInfo $memberInfo); ),但是我对PHP 5对象的处理有些动摇,所以我不确定可能出了什么问题。

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! 提前致谢!

so are you using the object after calling updateMember() ? 那么调用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. PHP5对象默认通过引用传递,因此,如果您在meta_data属性上调用json_encode() ,它将表现出您描述的行为。

you may want to post the updateMember() function to confirm, but it sounds like that's what's going on. 您可能需要发布updateMember()函数进行确认,但这听起来好像是这样。

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM