PHPackages                             ocramius/lazy-property - 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-property

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

ocramius/lazy-property
======================

A library that provides lazy instantiation logic for object properties

2.5.0(3y ago)6891.0k↓21.1%6[5 PRs](https://github.com/Ocramius/LazyProperty/pulls)3MITPHPPHP ~8.1.0 || ~8.2.0

Since Feb 19Pushed 2y ago2 watchersCompare

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

READMEChangelog (7)Dependencies (4)Versions (24)Used By (3)

Lazy Property
=============

[](#lazy-property)

This small library aims at providing a very simple and efficient loading of lazy properties

[![Latest Stable Version](https://camo.githubusercontent.com/5a854395a4eea553698b4c6b7922fefadd97a905aa894b43f50e86e795e11f45/68747470733a2f2f706f7365722e707567782e6f72672f6f6372616d6975732f6c617a792d70726f70657274792f762f737461626c652e706e67)](https://packagist.org/packages/ocramius/lazy-property)[![Latest Unstable Version](https://camo.githubusercontent.com/34510cd5b64ccd2f5ae094902dd7bb15aeac20dc2ecdc19392a47efafc605079/68747470733a2f2f706f7365722e707567782e6f72672f6f6372616d6975732f6c617a792d70726f70657274792f762f756e737461626c652e706e67)](https://packagist.org/packages/ocramius/lazy-property)

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-property
```

Use case
--------

[](#use-case)

In many cases where lazy-initialization of private/protected properties is necessary, many people write classes that look like following:

```
class SomeService
{
    protected $dependency;

    public function doWork()
    {
        $this->getDependency()->delegateWork();
    }

    protected function getDependency()
    {
        return $this->dependency ?: $this->dependency = get_dependency_somehow();
    }
}
```

This is problematic because implementors and people subclassing `SomeService` will eventually write:

```
class SomethingElse extends SomeService
{
    public function doOtherWork()
    {
        $this->dependency->doMoreWork();
    }
}
```

This can work only if `SomeService#getDependency()` was called at least once upfront (which may well be under certain circumstances), and therefore is a cause of bugs/headaches/suicides/etc.

In order to avoid this problem, the implementor of `SomeService` that is also exposing its protected `$dependency` property may just use `LazyProperty\LazyPropertiesTrait` to fix the problem:

```
#[\AllowDynamicProperties]
class SomeService
{
    use \LazyProperty\LazyPropertiesTrait;

    protected MyDependency $dependency;

    public function __construct()
    {
        $this->initLazyProperties(['dependency']);
    }

    public function doWork()
    {
        // look ma! no getter!
        $this->dependency->delegateWork();
    }

    protected function getDependency()
    {
        return $this->dependency ?: $this->dependency = get_dependency_somehow();
    }
}
```

With this, any access to `SomeService#$dependency` will cause a call to `SomeService#getDependency()` if the property was not already initialized.

```
class SomethingElse extends SomeService
{
    public function doOtherWork()
    {
        // always works
        $this->dependency->doMoreWork();
    }
}
```

Please note that a getter is *required* in order for the property to be lazy.

Performance notes
-----------------

[](#performance-notes)

Using `LazyProperty\LazyPropertiesTrait` allows to speed up applications where a massive amount of getter calls is going on in private/protected scope. There is some minor overhead in calling `SomeService#initLazyProperties()`, as well as in the first property access, but it should be negligible.

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity44

Moderate usage in the ecosystem

Community22

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 ~250 days

Recently: every ~81 days

Total

15

Last Release

972d ago

Major Versions

1.0.1 → 2.0.02019-03-04

PHP version history (7 changes)1.0.0PHP ~5.4

1.0.1PHP ~5.4|~7.0

2.0.0PHP ^7.3

2.1.0PHP ^7.4 || ^8.0

2.2.0PHP ^8.0

2.4.0PHP ~8.0.0 || ~8.1.0

2.5.0PHP ~8.1.0 || ~8.2.0

### Community

Maintainers

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

---

Top Contributors

[![Ocramius](https://avatars.githubusercontent.com/u/154256?v=4)](https://github.com/Ocramius "Ocramius (107 commits)")[![renovate[bot]](https://avatars.githubusercontent.com/in/2740?v=4)](https://github.com/renovate[bot] "renovate[bot] (48 commits)")[![nschoellhorn](https://avatars.githubusercontent.com/u/3925180?v=4)](https://github.com/nschoellhorn "nschoellhorn (38 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (29 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (20 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (20 commits)")[![glensc](https://avatars.githubusercontent.com/u/199095?v=4)](https://github.com/glensc "glensc (1 commits)")

---

Tags

lazy loadingutilitylazylazy instantiation

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[ocramius/proxy-manager

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

5.0k82.4M230](/packages/ocramius-proxy-manager)[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k394.3M1.5k](/packages/nette-utils)[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)[danielstjules/stringy

A string manipulation library with multibyte support

2.4k26.0M191](/packages/danielstjules-stringy)[voku/arrayy

Array manipulation library for PHP, called Arrayy!

4875.5M16](/packages/voku-arrayy)

PHPackages © 2026

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