PHPackages                             ocramius/lazy-map - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ocramius/lazy-map

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

ocramius/lazy-map
=================

A library that provides lazy instantiation logic for a map of objects

2.8.0(3y ago)88482.3k↓21.4%2[6 PRs](https://github.com/Ocramius/LazyMap/pulls)3MITPHPPHP ~8.1.0 || ~8.2.0

Since Nov 9Pushed 2y ago2 watchersCompare

[ Source](https://github.com/Ocramius/LazyMap)[ Packagist](https://packagist.org/packages/ocramius/lazy-map)[ Docs](https://github.com/Ocramius/LazyMap)[ GitHub Sponsors](https://github.com/Ocramius)[ RSS](/packages/ocramius-lazy-map/feed)WikiDiscussions 2.9.x Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (25)Used By (3)

Lazy Map
========

[](#lazy-map)

This small library aims at providing a very simple and efficient map of lazy-instantiating objects.

[![Total Downloads](https://camo.githubusercontent.com/499de69aa42e4e5598c6e85a7375dea1606c3c8ead12a7f6c88fe9a2c6444d37/68747470733a2f2f706f7365722e707567782e6f72672f6f6372616d6975732f6c617a792d6d61702f646f776e6c6f6164732e706e67)](https://packagist.org/packages/ocramius/lazy-map)[![Latest Stable Version](https://camo.githubusercontent.com/f1524db63e64479dfb4db6a8865421dc22508e48cd7d580cf4402586619bad26/68747470733a2f2f706f7365722e707567782e6f72672f6f6372616d6975732f6c617a792d6d61702f762f737461626c652e706e67)](https://packagist.org/packages/ocramius/lazy-map)[![Latest Unstable Version](https://camo.githubusercontent.com/df96608d51aee3648d77554bfbf5aef025ddea7144114cad496d2fe9d0566ed7/68747470733a2f2f706f7365722e707567782e6f72672f6f6372616d6975732f6c617a792d6d61702f762f756e737461626c652e706e67)](https://packagist.org/packages/ocramius/lazy-map)[![Mutation testing badge](https://camo.githubusercontent.com/257dff7c746259a8c578dcdb254cab6c9ebfcc6f9787cd0c09f716c196ebbf67/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d2532464f6372616d6975732532464c617a794d6170253246322e352e78)](https://dashboard.stryker-mutator.io/reports/github.com/Ocramius/LazyMap/2.5.x)

Abandoned
---------

[](#abandoned)

Starting with PHP 8.3, dynamic properties are [no longer allowed "out of the box"](https://wiki.php.net/rfc/deprecate_dynamic_properties). While it is still possible to have dynamic properties via explicit declaration (the `#[\AllowDynamicProperties]` attribute), the approach of this package is no longer to be considered safe nor efficient long-term.

Based on that, this package is deprecated and abandoned: please use traditional PHP `array`s instead.

Installation
------------

[](#installation)

The suggested installation method is via [composer](https://getcomposer.org/):

```
composer require ocramius/lazy-map
```

Usage
-----

[](#usage)

The current implementation is very simple and allows to define a map of "services" through a `LazyMap\CallbackLazyMap`:

```
$map = new \LazyMap\CallbackLazyMap(function ($name) {
    $object = new \stdClass();

    $object->name = $name;

    return $object;
});

var_dump($map->foo);
var_dump($map->bar);
var_dump($map->{'something special'});
```

Purpose
-------

[](#purpose)

The idea behind the library is to avoid un-efficient lazy-loading operations like following:

```
private function getSomething($name)
{
    if (isset($this->initialized[$name]) || array_key_exists($name, $this->initialized)) {
        return $this->initialized[$name];
    }

    return $this->initialized[$name] = new Something($name);
}
```

This reduces overhead greatly when you'd otherwise call `getSomething()` thousands of times. That's especially useful when mapping a lot of different services and iterating over them over and over again.

Performance
-----------

[](#performance)

LazyMap actually performs much better than the "un-efficient" example that I've shown above. You can look directly at the performance test suite for details on the tested implementations, but here are some results for you to have an idea of the boost:

#### Initialized Map Performance:

[](#initialized-map-performance)

Method NameOps/sRelativeinitializedArrayPerformance2,277,272.90002100.00%initializedArrayMapPerformance1,536,988.76108148.16%initializedLazyMapPerformance4,446,227.2351451.22%#### Un-Initialized Map Performance:

[](#un-initialized-map-performance)

Method NameOps/sRelativeunInitializedArrayPerformance :1,091,720.80627100.00%unInitializedArrayMapPerformance688,132.30083158.65%unInitializedLazyMapPerformance:912,191.90744119.68%

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity46

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity85

Battle-tested with a long release history

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~212 days

Recently: every ~81 days

Total

18

Last Release

972d ago

Major Versions

1.0.0 → 2.0.02019-08-11

PHP version history (7 changes)1.0.0PHP &gt;=5.3.3

2.0.0PHP ^7.3

2.1.0PHP ^7.4

2.2.0PHP ^7.4 || ^8.0

2.3.0PHP ^8.0

2.7.0PHP ~8.0.0 || ~8.1.0

2.8.0PHP ~8.1.0 || ~8.2.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/fd0445bc21fa116c259b5889377b90cbd8a34d49357321f76a74f6d2c2ae6b0c?d=identicon)[Ocramius](/maintainers/Ocramius)

---

Top Contributors

[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (330 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (323 commits)")[![Ocramius](https://avatars.githubusercontent.com/u/154256?v=4)](https://github.com/Ocramius "Ocramius (133 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (78 commits)")[![renovate[bot]](https://avatars.githubusercontent.com/in/2740?v=4)](https://github.com/renovate[bot] "renovate[bot] (65 commits)")[![hvt](https://avatars.githubusercontent.com/u/337754?v=4)](https://github.com/hvt "hvt (1 commits)")

---

Tags

lazy loadingmaplazylazy instantiationservice location

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/ocramius-lazy-map/health.svg)

```
[![Health](https://phpackages.com/badges/ocramius-lazy-map/health.svg)](https://phpackages.com/packages/ocramius-lazy-map)
```

###  Alternatives

[ocramius/proxy-manager

A library providing utilities to generate, instantiate and generally operate with Object Proxies

5.0k82.4M230](/packages/ocramius-proxy-manager)[symfony/var-exporter

Provides tools to export, instantiate, hydrate, clone and lazy-load PHP objects

2.1k378.1M441](/packages/symfony-var-exporter)[friendsofphp/proxy-manager-lts

Adding support for a wider range of PHP versions to ocramius/proxy-manager

1.2k139.1M104](/packages/friendsofphp-proxy-manager-lts)[phpcollection/phpcollection

General-Purpose Collection Library for PHP

1.0k64.0M34](/packages/phpcollection-phpcollection)[dasprid/enum

PHP 7.1 enum implementation

379146.0M11](/packages/dasprid-enum)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49444.8M97](/packages/marc-mabe-php-enum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
