Download the PHP package nxtlvlsoftware/static-constructors without Composer

On this page you can find all versions of the php package nxtlvlsoftware/static-constructors. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package static-constructors

Project Banner (nxtlvlsoftware/static-constructors)

Static Constructors for PHP

Brings static class initialization to PHP!

Build Status Coverage Status Total Downloads Latest Release License







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:

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:

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

Install with composer on the command line:

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

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.

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

A good summary of why you would want to use static constructors is available in this blog post by @ImLiam. The article is based on another, older package which inspired the __constructStatic() magic method in this package.

Contributing

Issues

Found a problem with this project? Make sure to open an issue on the issue tracker and we'll do our best to get it sorted!

License Information

nxtlvlsoftware/php-static-constructors is open-sourced software, freely available to use under the terms of the MIT License.

A full copy of the license is available here.

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 product.


All versions of static-constructors with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package nxtlvlsoftware/static-constructors contains the following files

Loading the files please wait ....