php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79195 "callable" not a valid type hint for class properties.
Submitted: 2020-01-30 03:30 UTC Modified: 2020-01-30 04:43 UTC
From: a at b dot c dot de Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 7.4.2 OS: Windows 10
Private report: No CVE-ID: None
 [2020-01-30 03:30 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-01-30 04:43 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2020-01-30 04:43 UTC] requinix@php.net
See the RFC for why callable is not supported.
https://wiki.php.net/rfc/typed_properties_v2#supported_types
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Jul 04 16:01:27 2020 UTC