{"id":23709919,"url":"https://github.com/nxtlvlsoftware/php-static-constructors","last_synced_at":"2026-03-05T19:02:16.310Z","repository":{"id":57029484,"uuid":"171633671","full_name":"NxtLvLSoftware/php-static-constructors","owner":"NxtLvLSoftware","description":"Brings static class constructors to PHP!","archived":false,"fork":false,"pushed_at":"2024-09-03T22:28:58.000Z","size":160,"stargazers_count":15,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"dev","last_synced_at":"2025-09-03T16:47:52.046Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://nxtlvlsoftware.github.io/php-static-constructors/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NxtLvLSoftware.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2019-02-20T08:39:14.000Z","updated_at":"2023-09-12T03:14:49.000Z","dependencies_parsed_at":"2025-09-03T16:32:18.080Z","dependency_job_id":"04d1a90c-026b-404f-85c7-35f37ce7007e","html_url":"https://github.com/NxtLvLSoftware/php-static-constructors","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/NxtLvLSoftware/php-static-constructors","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NxtLvLSoftware%2Fphp-static-constructors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NxtLvLSoftware%2Fphp-static-constructors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NxtLvLSoftware%2Fphp-static-constructors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NxtLvLSoftware%2Fphp-static-constructors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NxtLvLSoftware","download_url":"https://codeload.github.com/NxtLvLSoftware/php-static-constructors/tar.gz/refs/heads/dev","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NxtLvLSoftware%2Fphp-static-constructors/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30144700,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-05T16:58:46.102Z","status":"ssl_error","status_checked_at":"2026-03-05T16:58:45.706Z","response_time":93,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-30T18:58:55.034Z","updated_at":"2026-03-05T19:02:16.286Z","avatar_url":"https://github.com/NxtLvLSoftware.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://nxtlvlsoftware.github.io/php-static-constructors/\"\u003e\u003cpicture\u003e\n    \u003csource media=\"(prefers-color-scheme: dark)\" srcset=\"https://raw.githubusercontent.com/NxtLvLSoftware/php-static-constructors/dist/.github/banner-dark.png\"\u003e\n    \u003csource media=\"(prefers-color-scheme: light)\" srcset=\"https://raw.githubusercontent.com/NxtLvLSoftware/php-static-constructors/dist/.github/banner-light.png\"\u003e\n    \u003cimg alt=\"Project Banner (nxtlvlsoftware/static-constructors)\" src=\"https://raw.githubusercontent.com/NxtLvLSoftware/php-static-constructors/dist/.github/banner-light.png\" width=\"350\" height=\"160\" style=\"max-width: 100%;\"\u003e\n  \u003c/picture\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003ch1 align=\"center\"\u003e\n  Static Constructors for PHP\n\u003c/h1\u003e\n\n\u003ch4 align=\"center\" style=\"font-style: italic;\"\u003e\n  Brings static class initialization to PHP!\n\u003c/h4\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/NxtLvlSoftware/php-static-constructors/actions\"\u003e\u003cimg src=\"https://img.shields.io/github/actions/workflow/status/NxtLvlSoftware/php-static-constructors/ci.yml?branch=dev\" alt=\"Build Status\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://nxtlvlsoftware.github.io/php-static-constructors/coverage/\"\u003e\u003cimg src=\"https://nxtlvlsoftware.github.io/php-static-constructors/coverage/badge.svg\" alt=\"Coverage Status\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://packagist.org/packages/nxtlvlsoftware/static-constructors\"\u003e\u003cimg src=\"https://img.shields.io/packagist/dt/NxtLvlSoftware/static-constructors.svg\" alt=\"Total Downloads\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://github.com/NxtLvlSoftware/php-static-constructors/releases\"\u003e\u003cimg src=\"https://img.shields.io/packagist/v/NxtLvlSoftware/static-constructors.svg\" alt=\"Latest Release\"\u003e\u003c/a\u003e\n    \u003ca href=\"https://github.com/NxtLvlSoftware/php-static-constructors/blob/dev/LICENSE\"\u003e\u003cimg src=\"https://img.shields.io/packagist/l/NxtLvlSoftware/static-constructors\" alt=\"License\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cbr\u003e\n\u003chr\u003e\n\u003cbr\u003e\n\n* [Documentation](https://nxtlvlsoftware.github.io/php-static-constructors/docs/)\n* [Coverage Report](https://nxtlvlsoftware.github.io/php-static-constructors/coverage/)\n* [About](#about)\n* [Installation](#installation)\n* [Under The Hood](#under-the-hood)\n* [More Information](#more-information)\n* [Contributing](#contributing)\n  * [Issues](#issues)\n* [License](#license-information)\n\n\u003cbr\u003e\n\u003chr\u003e\n\u003cbr\u003e\n\n### About\n\nThis package brings a feature present in most other popular programming languages\nto PHP, static constructors. This is implemented using policies which search\nclasses for a suitable static method. The default policies look for private methods\nwith the same name as the class (case-sensitive) or a PHP-style magic `__constructStatic()`.\nThe next steps are making sure the method accepts no arguments, is not abstract and is\nuser defined (don't want to accidentally call something from an extension!)\n\nHere's a quick singleton example:\n```php\n// src/Example.php\nclass Example {\n\n    private static self $instance = null;\n    \n    public static function get(): self {\n        return self::$instance;\n    }\n\n    private static function Example(): void {\n        self::$instance = new self();\n    }\n\n    public function echo(): void {\n        echo \"Hello World!\" . PHP_EOL;\n    }\n\n}\n\n// src/bootstrap.php\nrequire_once __DIR__ . '/../vendor/autoload.php';\nExample::get()-\u003eecho();\n```\n\nYou can probably already guess the output of this code, the console will output\n`Hello World!`. The first time the class or any child classes are referenced in\nyour code the static constructor will be called. Parent constructors will always\nbe called first due to the way PHP loads classes:\n```php\nabstract class Parent {\n    private static function __constuctStatic() { echo \"Called first\" . PHP_EOL; }\n}\n\nclass Child extends Parent {\n    private static function __constructStatic() { echo \"Not called\" . PHP_EOL; }\n    private static function Child() { echo \"Called second\" . PHP_EOL; }\n}\n\n\n// src/bootstrap.php\nrequire_once __DIR__ . '/../vendor/autoload.php';\n$object = new Child;\n```\n\nMethod name and visibility requirments can be customised through the use of policies.\nIt is not recommended to do this for packages as it introduces incompatibility issues\nwhen other code relies on default behaviours. This feature is best put to use in\nframeworks where one of the project goals is to provide a custom, well documented\nenvironment.\n\n### Installation\n\nInstall with composer on the command line:\n\n```bash\n$ composer require nxtlvlsoftware/static-constructors\n```\n\nOr add the dependency directly to your composer.json manifest:\n\n```json\n{\n  \"require\": {\n    \"nxtlvlsoftware/static-constructors\": \"^1.0.0\"\n  }\n}\n```\n\nComposer will automatically handle 'hooking' the library for you when you require\nyour `vendor/autoload.php` file. If you use another autoloader in your project you\ncan disable the automatic hook by adding `define('DISABLE_STATIC_CONSTRUCTOR_HOOK', true)`\nanywhere before you `include` the composer autoload file. You must then hook the\nlibrary yourself with `\\NxtlvlSoftware\\StaticConstructors\\Loader::init()`. The\n`$_SERVER['DISABLE_STATIC_CONSTRUCTOR_HOOK']` and `$_ENV['DISABLE_STATIC_CONSTRUCTOR_HOOK']`\nglobals are checked for a `true` value as well.\n\nWhen manually initializing the loader\nyou can choose which policies to enable for the method name resolution and method\nrequirments. It is also possible to control whether the loader should search classes\nwhich are already declared in the runtime.\n```php\nuse \\NxtlvlSoftware\\StaticConstructors\\Loader;\n\nLoader::init(\n    // same as default\n    classPolicies: Loader::DEFAULT_CLASS_POLICIES,\n    // don't check if method is public/protected/private\n    methodPolicies: [],\n    // only check classes that the runtime loads after init call\n    checkLoadedClasses: false\n);\n```\n\n### Under the hood\n\nThis is all implemented by registering our own class loader and unregistering any\npreviously registered loaders. When our loader is called we do the job PHP normally\ndoes, loop over all the actual class loaders to try and load the class but this is where\nwe perform the magic that allows us to use static constructors. If a class is loaded by\none of the real loaders, we look for a suitable static constructor method on the loaded\nclass using reflection. If we find a constructor, we simply call it! PHP does most of the\ndirty work for us. If a class extends another then the parent class is automatically\nloaded by PHP before the child, so we don't even have to walk up the inheritance chain\nourselves. Since PHP only uses autoloader's to load a class into the runtime once, the\nconstructors are only ever called the first time the class is referenced.\n\n### More Information\nA 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)\nby [@ImLiam](https://github.com/ImLiam). The article is based on [another](https://github.com/vladimmi/construct-static),\nolder package which inspired the `__constructStatic()` magic method in this package.\n\n## Contributing\n\n#### Issues\n\nFound a problem with this project? Make sure to open an issue on the [issue tracker](https://github.com/NxtLvLSoftware/php-static-constructors/issues)\nand we'll do our best to get it sorted!\n\n## License Information\n\n[`nxtlvlsoftware/php-static-constructors`](https://github.com/NxtLvlSoftware/php-static-constructors)\nis 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).\n\n__A full copy of the license is available [here](https://github.com/NxtLvLSoftware/php-static-constructors/blob/dev/LICENSE).__\n\n\u003e THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n\u003e IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n\u003e FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n\u003e AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n\u003e LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n\u003e OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n\u003e SOFTWARE.\n\n\u003cbr\u003e\n\u003chr\u003e\n\u003cbr\u003e\n\n__A [NxtLvL Software Solutions](https://github.com/NxtLvLSoftware) product.__\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnxtlvlsoftware%2Fphp-static-constructors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnxtlvlsoftware%2Fphp-static-constructors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnxtlvlsoftware%2Fphp-static-constructors/lists"}