Download the PHP package upscale/stdlib-overloading without Composer
On this page you can find all versions of the php package upscale/stdlib-overloading. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download upscale/stdlib-overloading
More information about upscale/stdlib-overloading
Files in upscale/stdlib-overloading
Package stdlib-overloading
Short Description PHP7 function/method arguments overloading
License Apache-2.0
Homepage https://github.com/upscalesoftware/stdlib-overloading
Informations about the package stdlib-overloading
Function Overloading 
This library introduces function/method overloading – varying implementation depending on input arguments.
Features:
- Overloading by argument types
- Overloading by number of arguments
- Efficient native type checks of PHP7
- Informative native error messages of
TypeError
- Lightweight: no OOP, no Reflection
Installation
The library is to be installed via Composer as a dependency:
Usage
Syntax
Overload a custom function/method:
Any number of valid callable
implementations can be declared. Order defines evaluation priority.
Call the overloaded function:
Example
Arithmetic calculator that works with ordinary integers, arbitrary-length GMP integers, and Money objects.
Architecture
The overloading mechanism leverages the native type system of PHP7 and relies on declaration of strict type annotations. It traverses implementation callbacks in the declared order and attempts to invoke each of them with provided arguments. Result of the first compatible invocation is returned and the subsequent callbacks discarded.
Limitations
PHP engine allows to pass more runtime arguments to a function/method than accounted for in its signature declaration.
Excess arguments are being silently discarded without triggering any catchable errors, not even ArgumentCountError
.
The solution is to declare more specific longer signatures before less specific ones with matching subset of arguments.
Optional arguments are problematic as they are call-time compatible with a shorter signature of required arguments only. Consider the following ambiguous declaration that cannot be resolved by reordering:
The workaround is to validate the number of arguments to not exceed the declaration:
Contributing
Pull Requests with fixes and improvements are welcome!
License
Copyright © Upscale Software. All rights reserved.
Licensed under the Apache License, Version 2.0.