php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69165 Passing result of assignation by reference
Submitted: 2015-03-02 23:48 UTC Modified: 2017-12-09 14:08 UTC
From: contact at jubianchi dot fr Assigned:
Status: Not a bug Package: Variables related
PHP Version: master-Git-2015-03-02 (Git) OS:
Private report: No CVE-ID: None
 [2015-03-02 23:48 UTC] contact at jubianchi dot fr
Description:
------------
There seems to be a bug when passing the result of an assignation to a function expecting a reference.

I extracted a snippet from atoum where we use a trick to verify if a variable is a reference to another and saw from the tests that PHP7's behavior changed

Test script:
---------------
<?php

// http://3v4l.org/UrMbA

function cmp(& $reference, & $value) {
    $tmp = $reference;
	$reference = uniqid(mt_rand());
	$isReference = ($value === $reference);
	$reference = $tmp;  
	
	return $isReference;
}

$value = uniqid();
$reference = & $value;
var_dump(cmp($reference, $value));

$value = new \exception();
var_dump(cmp($reference, $value));

var_dump(cmp($reference, $value = new \exception()));

Expected result:
----------------
bool(true)
bool(true)
bool(true)

Actual result:
--------------
bool(true)
bool(true)
bool(false)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-03-03 07:28 UTC] nikic@php.net
The old behavior looks like a bug to me. Assignments always return the (dereferenced) RHS *value*.
 [2017-12-09 14:08 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2017-12-09 14:08 UTC] nikic@php.net
Per previous comment, I believe the new behavior is correct, so I'm closing this. There used to be an exemption for passing objects by-ref due to this being common in PHP 4, but this special case no longer exists.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC