Bug #79195 [Opn->Nab]: "callable" not a valid type hint for class properties.
| From: | requinix@php.net | Date: | Thu, 30 Jan 2020 04:43:37 +0000 |
| Subject: | Bug #79195 [Opn->Nab]: "callable" not a valid type hint for class properties. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-225239@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
Updated by: requinix@php.net
Reported by: a at b dot c dot de
Summary: "callable" not a valid type hint for class
properties.
-Status: Open
+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:
See the RFC for why callable is not supported.
https://wiki.php.net/rfc/typed_properties_v2#supported_types
Previous Comments:
------------------------------------------------------------------------
[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