What if your constants didn't exist until you looked at them?

In PHP, a constant is usually… well, constant. Boring. Static. Defined once, and always the same.

But what if we injected a little quantum physics into our code — something that collapses into a value only when observed?

Meet heisenconstant(): a playful yet powerful utility inspired by Heisenberg's uncertainty principle, giving you lazy-evaluated, memoized, type-aware, introspectable constants with quantum flavor.

This feature is now part of the Dataphyre PHP framework — where performance, clarity, and originality converge at the architectural level.

Function comment


The Implementation

function heisenconstant(string $name, array $map): void {
    foreach($map as $key => $value){
        if($value instanceof Closure){
            $map[$key] = new class($value) {
                private Closure $fn;
                private mixed $cached = null;
                private bool $evaluated = false;

                public function __construct(Closure $fn){
                    $this->fn = $fn;
                }

                public function __toString(): string {
                    return (string)($this->cached ??= ($this->evaluate()));
                }

                public function toInt(): int {
                    return (int)($this->cached ??= ($this->evaluate()));
                }

                public function toFloat(): float {
                    return (float)($this->cached ??= ($this->evaluate()));
                }

                public function toArray(): array {
                    return (array)($this->cached ??= ($this->evaluate()));
                }

                public function toBool(): bool {
                    return (bool)($this->cached ??= ($this->evaluate()));
                }

                public function raw(): mixed {
                    return $this->cached ??= ($this->evaluate());
                }

                public function reset(): void {
                    $this->cached = null;
                    $this->evaluated = false;
                }

                private function evaluate(): mixed {
                    $this->evaluated = true;
                    return ($this->fn)();
                }

                public function __debugInfo(): array {
                    return [
                        'status' => $this->evaluated ? 'evaluated' : 'unevaluated',
                        'value' => $this->cached ?? '[not yet evaluated]',
                    ];
                }
            };
        }
    }
    define($name, $map);
}

How It Works

1. Define with Lazy Closures

heisenconstant('MY_CONST', [
    'uuid' => fn() => bin2hex(random_bytes(16)),
    'now'  => fn() => date('c'),
]);

Instead of precomputing values, we pass closures that won’t run until needed.


2. Collapse on Observation

echo MY_CONST['uuid']; // Evaluates once, then cached

The first time you observe a value — by casting it to a string — it executes the closure and remembers the result.


3. Introspect the Quantum State

var_dump(MY_CONST['now']);

Gives output like:

object(class@anonymous)#1 (2) {
  ["status"] => "evaluated"
  ["value"]  => "2025-04-10T23:10:00+00:00"
}

You can literally see whether a value has been observed or not.

Just like a cat in a box.


Bonus Features

These quantum values aren't just for printing. You can access them using:

  • toInt() – return the value cast to integer
  • toFloat() – cast to float
  • toArray() – cast to array
  • toBool() – cast to boolean
  • raw() – get the raw, unmodified value
  • reset() – uncollapse the state and force re-evaluation on next access

Each key in your constant behaves like its own particle, independently observed.


Why This Is Cool

  • Performance: Don’t compute unless you need it
  • Clean Debugging: __debugInfo() reveals state — no magic
  • Powerfully Weird: Constants that obey Schrödinger, not RFCs
  • Safe: Still defined with define(), just with quantum flavor
  • Composable: You can mix closures and static values

Real-World Use Cases

  • ROOTPATH['theme']: resolve theme path lazily at boot
  • SYSTEM_STATE['uuid']: guaranteed unique per boot, but only if accessed
  • DEBUG_SNAPSHOTS['config_dump']: snapshot config lazily for logs
  • BUILD_INFO['git_hash']: only needed in debug output or crash reports

Warning: Here Be Dragons

Heisenconstants are safe — until you start counting on them being unobserved.

If you rely on the closure not being triggered, be careful with:

  • Logging
  • Serialization
  • Third-party tools

Any cast to string or type observation collapses the quantum wave.

If you're unsure, use reset() to re-establish the superposition state.


Final Thoughts

heisenconstant() is a philosophical and practical coding artifact — a reminder that sometimes code should do less until it's asked to do more.

It turns PHP’s define() into a sandbox of potential, one where constants aren't constants… until they are.


About the Author

Jérémie Fréreault is the creator of Dataphyre and Shopiro, building from-scratch systems with zero third-party lock-in, brutal scalability, and a deep love for elegant engineering. He also occasionally weaponizes physics metaphors in production code. This is one of those times.

License: GitHub