php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #80032 Can't use valid callback as default value for callable parameters
Submitted: 2020-08-28 19:16 UTC Modified: 2020-10-11 04:22 UTC
From: antonfedonyuk at gmail dot com Assigned: cmb (profile)
Status: No Feedback Package: *General Issues
PHP Version: 7.4.9 OS: Any
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 — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
26 + 48 = ?
Subscribe to this entry?

 
 [2020-08-28 19:16 UTC] antonfedonyuk at gmail dot com
Description:
------------
Can't use valid callback(string or array) as default value of callable parameters. It's generate fatal error: Default value for parameters with callable type can only be NULL. The guide doesn't contain any notes on this issue.

Test script:
---------------
<?php
function test(callable $callback = 'strtoupper')
{
   return $callback();
}
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-08-28 21:01 UTC] requinix@php.net
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem -Package: Compile Failure +Package: *General Issues
 [2020-09-30 12:19 UTC] cmb@php.net
-Status: Verified +Status: Feedback -Assigned To: +Assigned To: cmb
 [2020-09-30 12:19 UTC] cmb@php.net
The manual states[1]:

| A function may define C++-style default values for scalar
| arguments as follows:

Since callables are not scalar, is there anything to add?

[1] <https://www.php.net/manual/en/functions.arguments.php#functions.arguments.default>
 [2020-09-30 15:37 UTC] antonfedonyuk at gmail dot com
-Status: Feedback +Status: Assigned
 [2020-09-30 15:37 UTC] antonfedonyuk at gmail dot com
cmb@php.net iterable not scalar too
 [2020-09-30 15:47 UTC] cmb@php.net
-Status: Assigned +Status: Feedback
 [2020-09-30 15:47 UTC] cmb@php.net
> iterable not scalar too

Well, the documentation mentions after the scalar examples:

| PHP also allows the use of arrays and the special type NULL as
| default values, […]
 [2020-10-11 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 07:01:31 2024 UTC