PHPackages                             slime-systems/object-id - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. slime-systems/object-id

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

slime-systems/object-id
=======================

A feature-packed, BSON-compatible ObjectId implementation for PHP, similar to MongoDB's ObjectId, providing unique ID generation, conversions, and comparison utilities.

1.0.2(7mo ago)031BSD-2-ClausePHPPHP &gt;=8.0CI passing

Since Nov 20Pushed 7mo agoCompare

[ Source](https://github.com/slime-systems/php-object-id)[ Packagist](https://packagist.org/packages/slime-systems/object-id)[ RSS](/packages/slime-systems-object-id/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (2)Versions (4)Used By (1)

Object ID
=========

[](#object-id)

A feature-packed, standalone **ObjectId** implementation for PHP.

[![Latest Version](https://camo.githubusercontent.com/d05859591514193b24c582d22fb7e58560565da9d0c83357dda2346274d28595/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736c696d652d73797374656d732f6f626a6563742d6964)](https://packagist.org/packages/slime-systems/object-id)[![Tests](https://github.com/slime-systems/php-object-id/actions/workflows/php.yml/badge.svg)](https://github.com/slime-systems/php-object-id/actions/workflows/php.yml)

---

**SlimeSystems\\ObjectId** provides a BSON-compatible identifier generator and utility class, fully compliant with the [MongoDB ObjectId specification](https://www.mongodb.com/docs/manual/reference/bson-types/#objectid).

It is lightweight, requires no external extensions (like `mongodb`), and is perfect for generating sortable, unique identifiers in any PHP application.

🚀 Installation
--------------

[](#-installation)

Install via Composer:

```
composer require slime-systems/object-id
```

⚡ Quick Start
-------------

[](#-quick-start)

```
use SlimeSystems\ObjectId;

// Generate a new unique ID
$id = new ObjectId();

echo $id;
// Output: 507f1f77bcf86cd799439011

// Get the generation time
$timestamp = $id->toTime();
// Returns a DateTime object
```

📖 Usage Guide
-------------

[](#-usage-guide)

### Creating ObjectIds

[](#creating-objectids)

#### Generate a New ID

[](#generate-a-new-id)

Create a fresh, unique 12-byte identifier.

```
$id = new ObjectId;
```

#### From Hex String

[](#from-hex-string)

Restore an ObjectId from its 24-character hexadecimal representation.

```
$id = ObjectId::fromString('507f1f77bcf86cd799439011');
```

#### From Binary Data

[](#from-binary-data)

Create from a raw 12-byte binary string (e.g., stored in a database).

```
$id = ObjectId::fromBinary($binaryData);
```

#### From Timestamp

[](#from-timestamp)

Generate an ID based on a specific time. useful for time-based sorting or filtering.

```
// From a DateTime object
$id = ObjectId::fromTime(new DateTime('2025-01-01'));

// From a Unix timestamp
$id = ObjectId::fromTime(1735689600);
```

**Note:** By default, `fromTime` generates a unique ID (randomizing the remaining bytes). If you need a "zeroed" ID for range queries (e.g., "find all IDs created after X"), pass `unique: false`:

```
$startId = ObjectId::fromTime($timestamp, unique: false);
// Last 8 bytes will be 0000000000000000
```

Invalid input to the `ObjectId::from...` series will throw `SlimeSystems\ObjectId\Exception\Invalid`. They are safe for handling untrusted variables, assuming that this exception is the expected behavior.

### Conversions

[](#conversions)

```
$id = new ObjectId;

// To Hex String (24 chars)
$hex = $id->toString();
// or simply: (string) $id

// To Binary (12 bytes)
$bin = $id->toBinary();

// To DateTime
$date = $id->toTime();
```

### Comparisons

[](#comparisons)

#### Equality Check

[](#equality-check)

Check if two ObjectIds represent the same value.

```
if ($id1->equals($id2)) {
    // ...
}
```

#### Sorting

[](#sorting)

Compare two IDs lexicographically (useful for sorting).

```
$result = $id1->compareTo($id2);
// -1 if $id1 < $id2
//  0 if $id1 == $id2
//  1 if $id1 > $id2
```

### Debugging

[](#debugging)

Get a readable inspection string.

```
echo $id->inspect();
// SlimeSystems\ObjectId(507f1f77bcf86cd799439011)
```

🔌 Framework Integration
-----------------------

[](#-framework-integration)

### Laravel / Eloquent

[](#laravel--eloquent)

Using Laravel? Check out **[SlimeSystems\\EloquentObjectId](https://github.com/slime-systems/eloquent-object-id)** for seamless integration with Eloquent models.

🧪 Testing
---------

[](#-testing)

Run the test suite with:

```
composer run test
```

Or if you have containerd:

```
make test
```

📄 License
---------

[](#-license)

This project is licensed under the [BSD 2-Clause License](./LICENSE.md).

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance63

Regular maintenance activity

Popularity3

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

3

Last Release

226d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/70a92a72c5e239c196a4407763deaa0330d9640c508353f30444c3ed946f58eb?d=identicon)[midnight-wonderer](/maintainers/midnight-wonderer)

---

Top Contributors

[![midnight-wonderer](https://avatars.githubusercontent.com/u/7634596?v=4)](https://github.com/midnight-wonderer "midnight-wonderer (19 commits)")

---

Tags

bsonbson-objectidmongodbunique-identifier

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/slime-systems-object-id/health.svg)

```
[![Health](https://phpackages.com/badges/slime-systems-object-id/health.svg)](https://phpackages.com/packages/slime-systems-object-id)
```

###  Alternatives

[illuminate/support

The Illuminate Support package.

630113.0M41.3k](/packages/illuminate-support)[spatie/holidays

Calculate public holidays

402860.1k2](/packages/spatie-holidays)[craftcms/feed-me

Import content from XML, RSS, CSV or JSON feeds into entries, categories, Craft Commerce products, and more.

293952.6k33](/packages/craftcms-feed-me)[solspace/craft-freeform

The most flexible and user-friendly form building plugin!

54681.3k19](/packages/solspace-craft-freeform)[pimcore/data-importer

Adds a comprehensive import functionality to Pimcore Datahub

46855.5k5](/packages/pimcore-data-importer)[flarum/core

Delightfully simple forum software.

201.4M2.3k](/packages/flarum-core)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
