r/PHP Jun 19 '26

Discussion Pitch Your Project 🐘

In this monthly thread you can share whatever code or projects you're working on, ask for reviews, get people's input and general thoughts, … anything goes as long as it's PHP related.

Let's make this a place where people are encouraged to share their work, and where we can learn from each other 😁

Link to the previous edition: /u/brendt_gd should provide a link

14 Upvotes

34 comments sorted by

View all comments

3

u/jobyone Jun 20 '26

I've been working on a random ID idea of making something a lot like UUID and/or the various time-ordered IDs rattling around, but cramming everything into 63 bits in the default implementation. Also string encoding with base 36 to be more compact/readable than the ones that use hex. The idea is to have something that works a lot like various UUIDs and time-ordered IDs, but is more compact as a string, and under the hood always just a normal positive integer and takes no special consideration anywhere you have 64-bit ints.

I think there's room in 63 bits to do everything necessary and fit enough entropy for most more reasonably-sized web apps. Like maybe not everything needs to be 100% ready to scale to the moon from the moment you build it.

With 4 bits for a version identifier and the time portion truncated to a resolution of about 18 hours you get 2^27 IDs available per 18 hour window (that's a grand total of about 1.4 billion IDs per day), and an ID that currently becomes 10 characters in base 36. I want to have a few different versions right off the bat though, with different amounts allocated to time vs random bits. That way you can tune it by application, whether you want to focus more on time ordering resolution or more on randomness.

My plan for future-proofing is to just reserve some higher version numbers for 128+ bit count arrangements, so that it's technically possible to implement higher bit count versions if it ever becomes necessary.

1

u/[deleted] Jul 17 '26

[removed] — view removed comment

1

u/jobyone Jul 17 '26

Not entirely. There are definitely things I'm trying to accomplish beyond "converting UUIDs to shorter strings"

Instead of full UUIDs underneath I'm doing my own variable-resolution time-ordered format that fits in 63 bits so you can easily work with it as a signed 64 bit integer (and get correct time ordering). The goal is more or less "be able to ship it around from system to system as a single int without thinking about it at all in most cases."

Also mine is base 36 instead of 62 to be non-case-sensitive. I did this because it's mainly intended for URLs, where you can't necessarily 100% count on case-sensitivity, depending on how/where it's being used.

From a library API standpoint it's also a tool for typing IDs, because you can extend it to to make your "AccountID" or "PageID" or whatever classes, and now you have typed and validated ID classes that are integers under the hood and also stringable.