-
Notifications
You must be signed in to change notification settings - Fork 0
HashMap
Ray Fung edited this page Feb 26, 2026
·
3 revisions
The HashMap class provides a hash-based object registry that implements ArrayAccess, Iterator, and Countable. Objects are stored and looked up by their spl_object_hash or an explicit hash key.
| Interface | Methods Provided |
| --- | --- |
| ArrayAccess | offsetExists, offsetGet, offsetSet, offsetUnset |
| Iterator | current, key, next, rewind, valid |
| Countable | count |
| Method | Return | Description |
| --- | --- | --- |
| push(mixed $object, string $hash = '') | void | Add an item; uses spl_object_hash if no hash given |
| getGenerator() | Generator | Lazy iteration via generator |
| remove(mixed $objectOrHash) | void | Remove by object reference or hash string |
| has(mixed $objectOrHash) | bool | Check if an object or hash exists |
use Razy\HashMap;
$map = new HashMap();
// Push objects (auto-hashed)
$objA = new stdClass();
$objB = new stdClass();
$map->push($objA);
$map->push($objB);
// Count
echo count($map); // 2
// Check existence
$map->has($objA); // true
// Remove
$map->remove($objA);
$map->has($objA); // false$map = new HashMap();
// Use custom hash keys for named lookups
$map->push($userService, 'user-service');
$map->push($authService, 'auth-service');
// Array access by hash
$service = $map['user-service'];
// Check by hash string
$map->has('auth-service'); // true
// Remove by hash string
$map->remove('auth-service');// Standard foreach (Iterator interface)
foreach ($map as $hash => $object) {
echo "$hash: " . get_class($object) . "\n";
}
// Lazy generator iteration (memory-efficient)
foreach ($map->getGenerator() as $hash => $object) {
// Process one at a time
}// offsetExists → isset check
isset($map['user-service']); // true
// offsetGet → retrieve by hash
$obj = $map['user-service'];
// offsetSet → add with hash key
$map['cache-service'] = $cacheService;
// offsetUnset → remove by hash
unset($map['cache-service']);use Razy\HashMap;
class ServiceContainer
{
private HashMap $services;
public function __construct()
{
$this->services = new HashMap();
}
public function register(string $name, object $service): void
{
$this->services->push($service, $name);
}
public function get(string $name): ?object
{
return $this->services->has($name)
? $this->services[$name]
: null;
}
}use Razy\HashMap;
$plugins = new HashMap();
// Register plugins by name
$plugins->push(new CachePlugin(), 'cache');
$plugins->push(new LoggerPlugin(), 'logger');
$plugins->push(new MetricsPlugin(), 'metrics');
// Initialize all plugins
foreach ($plugins->getGenerator() as $name => $plugin) {
$plugin->initialize();
echo "Loaded plugin: $name\n";
}
// Hot-swap a plugin at runtime
$plugins->remove('cache');
$plugins->push(new RedisCachePlugin(), 'cache');use Razy\HashMap;
$entityPool = new HashMap();
// Track entities by their auto-generated hash
$entity1 = new UserEntity(1, 'Alice');
$entity2 = new UserEntity(2, 'Bob');
$entityPool->push($entity1);
$entityPool->push($entity2);
// Check if a specific entity instance is tracked
if ($entityPool->has($entity1)) {
echo 'Entity is in pool (' . count($entityPool) . ' total)';
}