如何修复新的 stdClass 错误

How to fix new stdClass error

我试图从向函数发送数据的角度理解 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"}}}

像这样为每个引用创建 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);
}