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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: a at b dot c dot de
New email:
PHP Version: OS:

 

 [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: Thu Mar 28 09:01:26 2024 UTC