php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22847 Reference don't work in sort callbacks
Submitted: 2003-03-24 06:19 UTC Modified: 2005-01-25 15:31 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: jparneodo at yahoo dot fr Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4CVS, 5CVS (2004-04-13) OS: *
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: jparneodo at yahoo dot fr
New email:
PHP Version: OS:

 

 [2003-03-24 06:19 UTC] jparneodo at yahoo dot fr
function cmp($a,$b){
	global $g;
	var_dump($g);	// NULL here
	if($g[$a]>$g[$b]){
		return -1;
	}elseif($g[$a]<$g[$b]){
		return 1;
	}else{
		return 0;
	}
}
class A {
	var $aa=array('B'=>2,'A'=>1,'C'=>3);
	function sortme(){
		global $g;
		$g=&$this->aa;
		uksort($g,'cmp');
	}
}
$a=new A;
$a->sortme();		// Don't work

$g=&$a->aa;
uksort($g,'cmp');	// Work

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-08-06 19:16 UTC] iliaa@php.net
The problem can be replicated with a simpler script:
<?php
class A {
        var $b=1;
        function test()
        {
                global $g;
                $g =& $this->b;
        }
}
$n = new A();
$g = "BAR";
$n->test();
var_dump($g);
?>

The problem itself appears to be ZE's inability to make a variable imported using global a reference. if $GLOBALS['var_name'] is used the script works correctly
 [2005-01-25 15:31 UTC] sniper@php.net
RTFM

"true global variable imported inside a function scope with the global  statement actually creates a reference to the global variable. This can lead to unexpected behaviour which the following example addresses.."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 21 15:01:34 2024 UTC