Callable arguments cannot have default value

From: Date: Mon, 27 Nov 2023 23:59:22 +0000
Subject: Callable arguments cannot have default value
Groups: php.internals 
Request: Send a blank email to internals+get-121818@lists.php.net to get a copy of this message
Hi, Wanted to bring up an inconsistent behavior of callable arguments compared to arguments of other types. Callable argument cannot have a default value (tested string or array types - both are not permitted). The same exact value works perfectly fine when passed dynamically, it just cannot be specified as a default. The workaround is to remove the type annotation which is obviously undesirable. Here’s an example: declare(strict_types=1); function test(callable $idGenerator = 'session_create_id') { $id = $idGenerator(); // ... } The function/method declaration above produces the following error on all PHP versions: Fatal error: Cannot use string as default value for parameter $idGenerator of type callable in /tmp/preview on line 4 Note that the exact same string argument can be passed without any issue: function test(callable $idGenerator) {…} test('session_create_id’); Is there a specific architectural limitation causing this that's hard/impossible to overcome? I’m aware that class properties cannot be annotated with callable - another unfortunate limitation. Callable is not a real type like other primitive types which causes all these inconsistencies, correct? Callable properties (separate topic) may be a challenge, but can at least argument defaults be supported? Regards, Sergii Shymko

« previous php.internals (#121818) next »