php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #76904 New Closure class method suggestion
Submitted: 2018-09-19 17:16 UTC Modified: 2018-10-07 09:38 UTC
From: hello at rayfung dot hk Assigned: cmb (profile)
Status: No Feedback Package: *General Issues
PHP Version: 7.2.10 OS:
Private report: No CVE-ID: None
 [2018-09-19 17:16 UTC] hello at rayfung dot hk
Description:
------------
Closure::apply — Binds and calls the closure with an array of parameters

#Description
```
public mixed Closure::apply ( object $newthis [, array $param_arr ] )
```
Temporarily binds the closure to newthis, and calls it with any given array of parameters.

#Parameters
newthis
    The object to bind the closure to for the duration of the call.

param_arr
    The parameters to be passed to the callback, as an indexed array.

#Return Values
Returns the return value of the closure.

Test script:
---------------
$closure = function($name, $age) {
    return $name . ' is ' . $age . ' years old';
}

$closure->apply($three, ['Peter', 30]);

Expected result:
----------------
Peter is 30 years old


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-09-19 17:20 UTC] Wes dot example at example dot org
already exists.

$closure->call($newThis, ...$arguments);
 [2018-09-19 17:22 UTC] Wes dot example at example dot org
so, for instance

$closure->call($three, ...['Peter', 30]);
 [2018-09-20 12:52 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2018-09-20 12:52 UTC] cmb@php.net
Indeed, what's wrong with `::call()` and using argument unpacking
(aka. spread operator)?
 [2018-10-07 09:38 UTC] cmb@php.net
-Status: Feedback +Status: No Feedback
 [2018-10-07 09:38 UTC] cmb@php.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 "Open". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Dec 11 14:02:33 2024 UTC