Overview
KB
Technical FAQ
PHP Manual
CSS2 Manual
HTML Manual
JS Guide
JS Reference
PhpDock Manual
Nu-Coder Manual
PhpExpress Manual
PHP Joomla
Development
Learn PHP
 
<MongoDB\BSON\ObjectIdMongoDB\BSON\ObjectId::getTimestamp>
^MongoDB\BSON\ObjectIdLast updated: Mon, 26 Oct 2026

MongoDB\BSON\ObjectId::__construct

(mongodb >=1.0.0)

MongoDB\BSON\ObjectId::__construct — Construct a new ObjectId

Description

final public function MongoDB\BSON\ObjectId::__construct(?string $id = null)

Parameters

id (string)

A 24-character hexadecimal string. If not provided, the extension will generate an ObjectId.

Errors/Exceptions

Examples

Example #1 MongoDB\BSON\ObjectId::__construct() example

<?php

var_dump(new MongoDB\BSON\ObjectId());

var_dump(new MongoDB\BSON\ObjectId('000000000000000000000001'));

?>

The above example will output something similar to:

object(MongoDB\BSON\ObjectId)#1 (1) {
  ["oid"]=>
  string(24) "56732d3dda14d81214634921"
}
object(MongoDB\BSON\ObjectId)#1 (1) {
  ["oid"]=>
  string(24) "000000000000000000000001"
}

<MongoDB\BSON\ObjectIdMongoDB\BSON\ObjectId::getTimestamp>
^MongoDB\BSON\ObjectIdLast updated: Mon, 26 Oct 2026