Skip to content

SOF3/static-overloads

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

static-overloads

Use parameter-varying method overloads in PHP

Pass an array of functions with different signatures into the Overload constructor. Invoke the Overload object directly with an array of the parameters you got, and the library will determine the correct implementation to use based on the parameter types.

Example:

function my_func(...$args){
    // We are storing the Overload object as a static-scope variable.
    // This caches the analyzed implementation signatures to improve runtime performance
    // if the function is called frequently within the same runtime.
    static $overload = null; // due to PHP language limitations, $overload cannot be initialized inline.
    $overload = $overload ?? new Overload([
        function(string $a){
            implementation1($a);
        },
        function(int $a){
            implementation2($a);
        }
    ]);

Releases

No releases published

Packages

No packages published

Languages