rfc:typesafe-callable
Differences
This shows you the differences between two versions of the page.
Next revisionBoth sides next revision | |||
rfc:typesafe-callable [2016/04/04 22:09] – created dasprid | rfc:typesafe-callable [2016/04/05 19:38] – dasprid | ||
---|---|---|---|
Line 9: | Line 9: | ||
Currently, when you typehint against " | Currently, when you typehint against " | ||
- | This RFC tries to solve this problem by introducing | + | This RFC tries to solve this problem by introducing |
===== Proposal ===== | ===== Proposal ===== | ||
- | This RFC proposes | + | This RFC proposes the definition of user-defined callables. Since " |
<code php> | <code php> | ||
- | interface MyCallable extends | + | callable |
- | { | + | |
- | public function __invoke(string $foo) : int; | + | |
- | } | + | |
</ | </ | ||
- | Now any function or method could typehint against | + | With such a callable in place, the user can now typehint against |
<code php> | <code php> | ||
- | function foo(MyCallable | + | function foo(FilterCallable |
{ | { | ||
- | return $foo(' | + | return $filter(' |
} | } | ||
- | foo(function (string $foo) { return | + | foo(function (string $foo) : string |
</ | </ | ||
- | Of course, this works with any other kind of callable as well, be it a string, an array or an object. Even with the object, it would not have to implement the interface, since it is considered a special one due to the extension of the " | + | Of course, this works with any other kind of callable as well, be it a string, an array or an object. |
- | + | ||
- | Since we do have an interface though, which technically represents a class, any callable would have to allow to be also invoked by calling $callable-> | + | |
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== |
rfc/typesafe-callable.txt · Last modified: 2021/03/27 14:22 by ilutov