Skip to content

Instantly share code, notes, and snippets.

@sshymko
Last active February 16, 2020 04:45
Show Gist options
  • Save sshymko/c3780dbe47880edf442339a7e931ea95 to your computer and use it in GitHub Desktop.
Save sshymko/c3780dbe47880edf442339a7e931ea95 to your computer and use it in GitHub Desktop.
PHP7 function/method signature overloading
<?php
declare(strict_types=1);
function overload(callable ...$implementations): callable
{
return function (...$args) use ($implementations) {
$error = new \LogicException('Invalid overloaded implementations');
foreach ($implementations as $candidate) {
try {
return $candidate(...$args);
} catch (\TypeError $e) {
$error = $e;
}
}
throw $error;
};
}
@sshymko
Copy link
Author

sshymko commented Feb 13, 2020

Released the overloading implementation as a Composer package:
https://github.com/upscalesoftware/stdlib-overloading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment