查詢

MongoDB\BSON\Document::__toString()函式—用法及示例

「 將MongoDB\BSON\Document物件轉換為字串表示形式 」


函式名稱: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' => '[email protected]'
]);

// 呼叫__toString()方法將Document物件轉換為字串
echo $document->__toString();

輸出:

{"name":"John Doe","age":25,"email":"[email protected]"}

注意事項:

  • MongoDB\BSON\Document::__toString()方法返回的是一個JSON格式的字串,表示該Document物件的內容。
  • 如果Document物件包含巢狀的子文件或陣列,它們也會被轉換為JSON格式的字串。
  • 在使用echo或print輸出Document物件時,實際上是隱式呼叫了__toString()方法。
補充糾錯
熱門PHP函式
分享連結