如何修复新的stdClass错误

问题描述 投票:-1回答:1

我试图理解new stdClass在向函数发送数据方面,我创建了以下代码,我将得到一个错误,即说Warning: Creating default object from empty value,但是,我仍然得到json数据。如何修复错误以及为什么我得到它?

$send = new stdClass();
$send->user->id = '12121212121';
$send->message->attachment->type = 'file';
$send->message->attachment->image = 'png';
$res = process($send);

function process($send){
    $data = json_encode($send);
    print_r($data);
}

结果看起来像这样,并且在我提到的结果下面有一个错误:

{"user":{"id":"12121212121"},"message":{"attachment":{"type":"file","image":"png"}}}

php stdclass
1个回答
2
投票

像这样为每个引用创建stdClass

$send = new stdClass();
$send->user=new stdClass();
$send->user->id = '12121212121';
$send->message=new stdClass();
$send->message->attachment=new stdClass();
$send->message->attachment->type = 'file';
$send->message->attachment->image = 'png';
$res = process($send);

function process($send){
    $data = json_encode($send);
    print_r($data);
}
© www.soinside.com 2019 - 2024. All rights reserved.