php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73751 7.1.0 changed behaviour of &$this when used in callback
Submitted: 2016-12-15 18:18 UTC Modified: 2016-12-16 21:13 UTC
From: jamesin dot hongkong dot 1 at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.1.0 OS:
Private report: No CVE-ID: None
 [2016-12-15 18:18 UTC] jamesin dot hongkong dot 1 at gmail dot com
Description:
------------
https://3v4l.org/vMc1N contains the sample code where `&$this` as part of the callback invocation:

```
Hooks::run( 'foo', array( &$this, &$var ) );
```

causes a "Warning: Parameter 1 to {closure}() expected to be a reference, value given" on 7.1.0.

Running the provided sample code on 5.3.0 - 5.6.25, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.0.10 works as expected.

Re-declaring $this will pass on 7.1.0 and 5.3.0 - 5.6.25, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.0.10.

```
$foo = $this;
Hooks::run( 'foo', array( &$foo, &$var ) );
```

Test script:
---------------
https://3v4l.org/vMc1N 


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-12-16 21:13 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2016-12-16 21:13 UTC] nikic@php.net
This change is caused by https://wiki.php.net/rfc/this_var, which no longer allows taking a reference to $this (instead, you will get a value).

We changed call_user_func_array() in 7.1 to still perform the call (even though a warning is emitted) to make sure this and similar discrepancies do not cause BC breaks beyond additional warnings.

The correct way of fixing this is to stop using by-reference argument passing for this parameter. You don't need it since PHP 4.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 08:01:28 2024 UTC