函数名称: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()方法。