PHPackages                             nxtlvlsoftware/static-constructors - 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. nxtlvlsoftware/static-constructors

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

nxtlvlsoftware/static-constructors
==================================

Brings static class constructors to PHP!

1.0.0(2y ago)15501[1 PRs](https://github.com/NxtLvLSoftware/php-static-constructors/pulls)MITPHPPHP ^8.2.0

Since Sep 12Pushed 1y ago2 watchersCompare

[ Source](https://github.com/NxtLvLSoftware/php-static-constructors)[ Packagist](https://packagist.org/packages/nxtlvlsoftware/static-constructors)[ RSS](/packages/nxtlvlsoftware-static-constructors/feed)WikiDiscussions dev Synced 4d ago

READMEChangelog (1)Dependencies (6)Versions (4)Used By (0)

 [   ![Project Banner (nxtlvlsoftware/static-constructors)](https://raw.githubusercontent.com/NxtLvLSoftware/php-static-constructors/dist/.github/banner-light.png) ](https://nxtlvlsoftware.github.io/php-static-constructors/)

 Static Constructors for PHP
=============================

[](#--static-constructors-for-php)

####  Brings static class initialization to PHP!

[](#--brings-static-class-initialization-to-php)

 [![Build Status](https://camo.githubusercontent.com/3ccd145b7e6042082acd4293bede684fde7dd73f6f3997d40b3185234a4ed137/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f4e78744c766c536f6674776172652f7068702d7374617469632d636f6e7374727563746f72732f63692e796d6c3f6272616e63683d646576)](https://github.com/NxtLvlSoftware/php-static-constructors/actions) [![Coverage Status](https://camo.githubusercontent.com/ccc4677d4b1bf39a42df2807779ccd257a7ae0c12770fee48862c90b316c2e22/68747470733a2f2f6e78746c766c736f6674776172652e6769746875622e696f2f7068702d7374617469632d636f6e7374727563746f72732f636f7665726167652f62616467652e737667)](https://nxtlvlsoftware.github.io/php-static-constructors/coverage/) [![Total Downloads](https://camo.githubusercontent.com/54cf97222ef663be312cce030c7f23a33664fc6f0294f788fa3e464af82e3a54/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f4e78744c766c536f6674776172652f7374617469632d636f6e7374727563746f72732e737667)](https://packagist.org/packages/nxtlvlsoftware/static-constructors) [![Latest Release](https://camo.githubusercontent.com/043f584217811a58f5b115214733562dd65f18ef92197997ad2d4631c87ada13/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f4e78744c766c536f6674776172652f7374617469632d636f6e7374727563746f72732e737667)](https://github.com/NxtLvlSoftware/php-static-constructors/releases) [![License](https://camo.githubusercontent.com/4a5cf65a1f990272a329275059bc64f48dd3d69c1de57982eda49828928b42a7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f4e78744c766c536f6674776172652f7374617469632d636f6e7374727563746f7273)](https://github.com/NxtLvlSoftware/php-static-constructors/blob/dev/LICENSE)

---

- [Documentation](https://nxtlvlsoftware.github.io/php-static-constructors/docs/)
- [Coverage Report](https://nxtlvlsoftware.github.io/php-static-constructors/coverage/)
- [About](#about)
- [Installation](#installation)
- [Under The Hood](#under-the-hood)
- [More Information](#more-information)
- [Contributing](#contributing)
    - [Issues](#issues)
- [License](#license-information)

---

### About

[](#about)

This package brings a feature present in most other popular programming languages to PHP, static constructors. This is implemented using policies which search classes for a suitable static method. The default policies look for private methods with the same name as the class (case-sensitive) or a PHP-style magic `__constructStatic()`. The next steps are making sure the method accepts no arguments, is not abstract and is user defined (don't want to accidentally call something from an extension!)

Here's a quick singleton example:

```
// src/Example.php
class Example {

    private static self $instance = null;

    public static function get(): self {
        return self::$instance;
    }

    private static function Example(): void {
        self::$instance = new self();
    }

    public function echo(): void {
        echo "Hello World!" . PHP_EOL;
    }

}

// src/bootstrap.php
require_once __DIR__ . '/../vendor/autoload.php';
Example::get()->echo();
```

You can probably already guess the output of this code, the console will output `Hello World!`. The first time the class or any child classes are referenced in your code the static constructor will be called. Parent constructors will always be called first due to the way PHP loads classes:

```
abstract class Parent {
    private static function __constuctStatic() { echo "Called first" . PHP_EOL; }
}

class Child extends Parent {
    private static function __constructStatic() { echo "Not called" . PHP_EOL; }
    private static function Child() { echo "Called second" . PHP_EOL; }
}

// src/bootstrap.php
require_once __DIR__ . '/../vendor/autoload.php';
$object = new Child;
```

Method name and visibility requirments can be customised through the use of policies. It is not recommended to do this for packages as it introduces incompatibility issues when other code relies on default behaviours. This feature is best put to use in frameworks where one of the project goals is to provide a custom, well documented environment.

### Installation

[](#installation)

Install with composer on the command line:

```
$ composer require nxtlvlsoftware/static-constructors
```

Or add the dependency directly to your composer.json manifest:

```
{
  "require": {
    "nxtlvlsoftware/static-constructors": "^1.0.0"
  }
}
```

Composer will automatically handle 'hooking' the library for you when you require your `vendor/autoload.php` file. If you use another autoloader in your project you can disable the automatic hook by adding `define('DISABLE_STATIC_CONSTRUCTOR_HOOK', true)`anywhere before you `include` the composer autoload file. You must then hook the library yourself with `\NxtlvlSoftware\StaticConstructors\Loader::init()`. The `$_SERVER['DISABLE_STATIC_CONSTRUCTOR_HOOK']` and `$_ENV['DISABLE_STATIC_CONSTRUCTOR_HOOK']`globals are checked for a `true` value as well.

When manually initializing the loader you can choose which policies to enable for the method name resolution and method requirments. It is also possible to control whether the loader should search classes which are already declared in the runtime.

```
use \NxtlvlSoftware\StaticConstructors\Loader;

Loader::init(
    // same as default
    classPolicies: Loader::DEFAULT_CLASS_POLICIES,
    // don't check if method is public/protected/private
    methodPolicies: [],
    // only check classes that the runtime loads after init call
    checkLoadedClasses: false
);
```

### Under the hood

[](#under-the-hood)

This is all implemented by registering our own class loader and unregistering any previously registered loaders. When our loader is called we do the job PHP normally does, loop over all the actual class loaders to try and load the class but this is where we perform the magic that allows us to use static constructors. If a class is loaded by one of the real loaders, we look for a suitable static constructor method on the loaded class using reflection. If we find a constructor, we simply call it! PHP does most of the dirty work for us. If a class extends another then the parent class is automatically loaded by PHP before the child, so we don't even have to walk up the inheritance chain ourselves. Since PHP only uses autoloader's to load a class into the runtime once, the constructors are only ever called the first time the class is referenced.

### More Information

[](#more-information)

A good summary of why you would want to use static constructors is available [in this blog post](https://liamhammett.com/static-constructors-in-php-y0zPVbQl)by [@ImLiam](https://github.com/ImLiam). The article is based on [another](https://github.com/vladimmi/construct-static), older package which inspired the `__constructStatic()` magic method in this package.

Contributing
------------

[](#contributing)

#### Issues

[](#issues)

Found a problem with this project? Make sure to open an issue on the [issue tracker](https://github.com/NxtLvLSoftware/php-static-constructors/issues)and we'll do our best to get it sorted!

License Information
-------------------

[](#license-information)

[`nxtlvlsoftware/php-static-constructors`](https://github.com/NxtLvlSoftware/php-static-constructors)is open-sourced software, freely available to use under the terms of the [MIT License](https://www.techtarget.com/whatis/definition/MIT-License-X11-license-or-MIT-X-license).

**A full copy of the license is available [here](https://github.com/NxtLvLSoftware/php-static-constructors/blob/dev/LICENSE).**

> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

---

**A [NxtLvL Software Solutions](https://github.com/NxtLvLSoftware) product.**

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance28

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity57

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

977d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/10399774?v=4)[Jack Noordhuis](/maintainers/JackNoordhuis)[@JackNoordhuis](https://github.com/JackNoordhuis)

---

Top Contributors

[![JackNoordhuis](https://avatars.githubusercontent.com/u/10399774?v=4)](https://github.com/JackNoordhuis "JackNoordhuis (53 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/nxtlvlsoftware-static-constructors/health.svg)

```
[![Health](https://phpackages.com/badges/nxtlvlsoftware-static-constructors/health.svg)](https://phpackages.com/packages/nxtlvlsoftware-static-constructors)
```

PHPackages © 2026

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