首页 话题 小组 问答 好文 用户 我的社区 域名交易 唠叨

[函数]MongoDB\BSON\Document::__toString()函数—用法及示例

发布于 2025-04-29 18:17:29
0
19

函数名称:MongoDB\BSON\Document::__toString()

适用版本:PHP 5 >= 5.6.0, PHP 7, PHP 8

函数描述:该函数用于将MongoDB\BSON\Document对象转换为字符串表示形式。

用法:

public MongoDB\BSON\Document::__toString(): string

示例:

// 创建一个MongoDB\BSON\Document对象
$document = new MongoDB\BSON\Document([
    'name' => 'John Doe',
    'age' => 25,
    'email' => 'johndoe@example.com'
]);

// 调用__toString()方法将Document对象转换为字符串
echo $document->__toString();

输出:

{"name":"John Doe","age":25,"email":"johndoe@example.com"}

注意事项:

  • MongoDB\BSON\Document::__toString()方法返回的是一个JSON格式的字符串,表示该Document对象的内容。
  • 如果Document对象包含嵌套的子文档或数组,它们也会被转换为JSON格式的字符串。
  • 在使用echo或print输出Document对象时,实际上是隐式调用了__toString()方法。
评论
一个月内的热帖推荐
啊龙
Lv.1普通用户

9545

帖子

31

小组

3242

积分

赞助商广告
站长交流