PHP code example of nxtlvlsoftware / static-constructors

1. Go to this page and download the library: Download nxtlvlsoftware/static-constructors library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nxtlvlsoftware / static-constructors example snippets


// 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

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

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
);