sof3/static-overloads

在PHP中使用参数变体方法重载

dev-master 2017-12-31 12:47 UTC

This package is auto-updated.

Last update: 2024-09-12 04:36:11 UTC


README

在PHP中使用参数变体方法重载

将具有不同签名的函数数组传递给Overload构造函数。直接使用获取的参数数组调用Overload对象,库将根据参数类型确定要使用的正确实现。

示例

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