Bug #79195 [Com]: "callable" not a valid type hint for class properties.

From: Date: Fri, 28 Aug 2020 19:33:37 +0000
Subject: Bug #79195 [Com]: "callable" not a valid type hint for class properties.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-228790@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79195&edit=1 ID: 79195 Comment by: antonfedonyuk at gmail dot com Reported by: a at b dot c dot de Summary: "callable" not a valid type hint for class properties. Status: Not a bug Type: Bug Package: Class/Object related Operating System: Windows 10 PHP Version: 7.4.2 Block user comment: N Private report: N New Comment: You can define "iterable" properties, but can't define "callable" properties. It's irrationally/illogically. Previous Comments: ------------------------------------------------------------------------ [2020-01-30 04:43:37] requinix@php.net See the RFC for why callable is not supported. https://wiki.php.net/rfc/typed_properties_v2#supported_types ------------------------------------------------------------------------ [2020-01-30 03:30:07] a at b dot c dot de Description: ------------ Of the list of available type declarations[1], callable is unique in that class properties cannot be declared as such. "Closure" can be used, but of course that prevents callables of other types (strings and arrays) from being stored. This may be a WONTFIX and a corresponding comment in the documentation saying "you can't do that", but since the more general "callable" type declaration exists and can and is used elsewhere, there seems a priori no reason why it can't be used as a property. [1] https://www.php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration Test script: --------------- <?php class Testing { private callable $foo; public function __construct($foo = null) { $this->foo = $foo; } } new Testing(fn($a) => $a); Expected result: ---------------- Nothing; script tests compilation only. Actual result: -------------- PHP Fatal error: Property Testing::$foo cannot have type callable in C:\tmp\test.php on line 4 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79195&edit=1

« previous php.bugs (#228790) next »