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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
19 + 22 = ?
Subscribe to this entry?

 
 [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
 [2020-08-28 19:33 UTC] antonfedonyuk at gmail dot com
You can define "iterable" properties, but can't define "callable" properties. It's irrationally/illogically.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jun 25 03:01:28 2024 UTC