naoyuki42/php-keyed-collection

A PHP library that provides a keyed collection class with type safety and immutability.

Maintainers

Package info

github.com/naoyuki42/php-keyed-collection

pkg:composer/naoyuki42/php-keyed-collection

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 1

0.1.0 2025-10-11 14:30 UTC

This package is auto-updated.

Last update: 2026-03-27 23:03:07 UTC


README

Overview

php-keyed-collection is a PHP library that provides a type-safe and immutable keyed collection class. It supports keys and values of any type, including arrays, objects, and null.

Features

  • Support for keys of any type (string, int, float, bool, array, object, null, etc.)
  • Methods for getting, setting, deleting, checking existence, clearing, and iterating values
  • Implements Countable and Iterator interfaces for foreach and count() support
  • Readonly size property for getting the number of elements

Installation

composer require naoyuki42/php-keyed-collection

Usage

use Naoyuki42\KeyedCollection\Map;

$map = new Map();

// Setting values
$map->set('key1', 'value1');
$map->set(123, 'value2');
$map->set([1,2], 'value3');
$obj = new stdClass();
$map->set($obj, 'value4');

// Getting values
echo $map->get('key1'); // value1
echo $map->get(123);    // value2

// Checking existence
var_dump($map->has([1,2])); // true

// Deleting values
$map->delete('key1');

// Getting size
echo $map->size; // 3

// Clearing all entries
$map->clear();

// Iteration
foreach ($map as $key => $value) {
    var_dump($key, $value);
}

License

MIT

日本語のREADME