PHPackages                             ali-eltaweel/lazy-props - 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. ali-eltaweel/lazy-props

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

ali-eltaweel/lazy-props
=======================

PHP Lazy-initialized properties

1.0.0(1y ago)0912PHPPHP ^8.1

Since Jun 20Pushed 1y agoCompare

[ Source](https://github.com/ali-eltaweel/lazy-props)[ Packagist](https://packagist.org/packages/ali-eltaweel/lazy-props)[ RSS](/packages/ali-eltaweel-lazy-props/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (1)Versions (2)Used By (2)

Lazy-Properties
===============

[](#lazy-properties)

- [Lazy-Properties](#lazy-properties)
    - [Installation](#installation)
    - [Usage](#usage)
        - [Declaring Lazy Properties](#declaring-lazy-properties)
            - [Common Initializer Method](#common-initializer-method)
        - [Subclasses's Readonly, Lazy-initialized Properties](#subclassess-readonly-lazy-initialized-properties)

---

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

[](#installation)

Install *lazy-props* via Composer:

```
composer require ali-eltaweel/lazy-props
```

Usage
-----

[](#usage)

### Declaring Lazy Properties

[](#declaring-lazy-properties)

```
use Lang\{ Annotations\LazyInitialized, LazyProperties };

class Database {

  use LazyProperties;

  #[LazyInitialized('createConnection')]
  public $connection;

  function createConnection() {

    return new stdClass();
  }
}
```

Upon instantiation of the `Database` class, the `connection` property will be unset, so that calls to `$db->connection` are directed to the `__get` magic method provided by the `LazyProperties` trait. This method will then call the `createConnection` method on the first call to `$db->connection`, and set the `connection` property to the return value of that method. Subsequent calls to `$db->connection` will return the already initialized value.

#### Common Initializer Method

[](#common-initializer-method)

If your class declares more than one lazy property, you can use a common initializer method for all of them:

```
class X {

  use LazyProperties;

  #[LazyInitialized('createArgument')]
  public int $x;

  #[LazyInitialized('createArgument')]
  public int $y;

  private function createArgument(string $name): int {

    echo "Initializing {$name}...\n";

    return match ($name) {
      'x' => 42,
      'y' => 84,
    };
  }
}
```

You can also you the `InitializerMethod` annotation on your class to save yourself some typing:

```
#[InitializerMethod('createArgument')]
class X {

  #[LazyInitialized()]
  public int $x;

  #[LazyInitialized()]
  public int $y;
}
```

---

### Subclasses's Readonly, Lazy-initialized Properties

[](#subclassess-readonly-lazy-initialized-properties)

Take a look at the following example:

```
class X {

  use LazyProperties;

  #[LazyInitialized('getX')]
  public int $x;

  private function getX() {

    return 42;
  }
}

class Y extends X {

  #[LazyInitialized('getY')]
  public int $y;

  // you can't make this initializer private, because it is called from the parent class.
  protected function getY() {

    echo "Initializing y...\n";
    return 84;
  }
}
```

It looks fine, and it is...

```
$y = new Y();

var_dump($y->y);
var_dump($y->y);
// "Initializing y..." is printed only once
```

But, when you change the `y` property to be `readonly`:

```
class Y extends X {

  #[LazyInitialized('getY')]
  public readonly int $y;
}
```

... you will get an error by just instantiating the `Y` class:

```
Cannot unset readonly property Y::$y from scope X

```

To fix this; each class has to unset (uninitialize) its own lazy properties, to do so, use the `LazyPropertiesExtension` trait in subclasses (of all generations) and annotate them with the `ExtendedLazyProperties` annotation giving the names of the extended lazy properties:

```
use Lang\{ Annotations\ExtendedLazyProperties, LazyPropertiesExtension };

#[ExtendedLazyProperties('y')]
class Y extends X {

  use LazyPropertiesExtension;

  #[LazyInitialized('getY')]
  public readonly int $y;
}
```

```
#[ExtendedLazyProperties('z')]
class Z extends Y {

  use LazyPropertiesExtension;

  #[LazyInitialized('getZ')]
  public readonly int $z;
}
```

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance48

Moderate activity, may be stable

Popularity10

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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

Unknown

Total

1

Last Release

379d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/45892756?v=4)[Ali Kamel](/maintainers/ali-eltaweel)[@ali-eltaweel](https://github.com/ali-eltaweel)

---

Top Contributors

[![ali-eltaweel](https://avatars.githubusercontent.com/u/45892756?v=4)](https://github.com/ali-eltaweel "ali-eltaweel (2 commits)")

### Embed Badge

![Health badge](/badges/ali-eltaweel-lazy-props/health.svg)

```
[![Health](https://phpackages.com/badges/ali-eltaweel-lazy-props/health.svg)](https://phpackages.com/packages/ali-eltaweel-lazy-props)
```

PHPackages © 2026

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