Last active
April 1, 2022 21:24
-
-
Save cspray/ec53ad92baa7d9829e21f7111d4eb919 to your computer and use it in GitHub Desktop.
A spike for having CallableInterfaces in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// In PHP internals, maybe this is an abstract class instead? | |
// Maybe an attribute? | |
#[Attribute] | |
class FunctionalInterface {} | |
#[FunctionalInterface] | |
interface MyAction { | |
public function invoke(string $foo, int $bar) : bool; | |
} | |
class ObjectAction implements MyAction { | |
public function invoke(string $foo, int $bar) : bool { | |
echoo $foo, $bar; | |
return true; | |
} | |
} | |
function doIt(MyAction $action) { | |
$action->invoke('foo', 42); | |
} | |
$goodFunc = function(string $a, int $b) : bool { | |
echo $b, $a; | |
return true; | |
}; | |
$badFunc = function() {}; | |
doIt(new ObjectAction()); // 'foo', 42 | |
doIt($goodFunc); // 42, 'foo' | |
doIt($badFunc); // exception, wrong method signature |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment