<?php
trait Singleton {
private static ?self $instance = null;
public static function instance(): static {
return self::$instance ??= new static();
}
public function __clone() { throw new LogicException('Cannot clone a singleton'); }
public function __wakeup() { throw new LogicException('Cannot unserialize a singleton'); }
private function __construct() {} // intentionally not callable from outside
}
final class Config { use Singleton; public string $env = 'prod'; }
echo Config::instance()->env; // prod
Config::instance()->env = 'dev';
echo Config::instance()->env; // dev — same instance
Create a free account and build your private vault. Share publicly whenever you want.