php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #23150 Using global $x, while $x is already global behaves in an inconsistent way
Submitted: 2003-04-10 09:20 UTC Modified: 2003-04-26 17:03 UTC
From: arattink at correct dot net Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.3.1 OS: Linux 2.4.16-4GB-SMP
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: arattink at correct dot net
New email:
PHP Version: OS:

 

 [2003-04-10 09:20 UTC] arattink at correct dot net
I would expect that using $x1->f = 1234; would work the 
same if $x1 is declared explicitely as global or not, if 
it is already global, but:

<?
class X
{
    var $f = 'PHP sucks';
};

$x1 = new X;
$x2 = new X;
$z1 = &$x1;
$z2 = &$x2;

{
    global $x1;
    $x1->f = 'PHP is OK!';
}

{
    $x2->f = 'PHP is OK!';
}

echo "x1.f=".$x1->f."\n";
echo "x2.f=".$x2->f."\n";
echo "z1.f=".$z1->f."\n";
echo "z2.f=".$z2->f."\n";

?>

shows this is not true. It seems that $z1 loses its 
reference to $x1, even though $x1 is never reassigned.

Tested on php 4.2.2 and php 4.3.1, same results.

List of modules: does not apply to this problem.

Known workaround: don't do this... However, it can easily 
happen by mistake (as it did in our case).

Yours sincerely, 
Arnoud Rattink.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-04-11 01:15 UTC] sniper@php.net
Not bug, works just as expected. (Don't do this :)

 [2003-04-11 01:16 UTC] sniper@php.net
.
 [2003-04-26 16:34 UTC] pollita@php.net
Reclassifying to Feature Request.

Calling global in the global scope should not be done.  It represents poor coding practices, however if someone on the Zend team wants to make this work (the basic problem is that the object is copied rather than referenced) here's the guys official feature request.
 [2003-04-26 17:01 UTC] gschlossnagle@php.net
The problem is with using global in the global scope.

The manual clearly states that 

global $x1;

is equivalent to 

$x1 =& $GLOBALS['x1'];

If you rewrite you example with that code to:


<?
class X
{
    var $f = 'PHP sucks';
};

$x1 = new X;
$x2 = new X;
$z1 = &$x1;
$z2 = &$x2;

{
    $x1 =& $GLOBALS['x1'];
//    global $x1;
    $x1->f = 'PHP is OK!';
}

{
    $x2->f = 'PHP is OK!';
}

echo "x1.f=".$x1->f."\n";
echo "x2.f=".$x2->f."\n";
echo "z1.f=".$z1->f."\n";
echo "z2.f=".$z2->f."\n";

?>

You can see that the problem is that $x1 gets redefined, 
leaving $z1 a reference to the original $x1.  Thus I'm 
marking this bug bogus.  The manual should probably be 
updated to say that not only is calling global in the 
global scope pointless, it may also not do what you think 
it does.

BTW: your attitude (as demonstrated in 23365) is piss-poor.  
PHP is a volunteer effort, calling someone 'ungrateful' for 
requesting a reproducing testcase is uncalled for.  If you 
want to be a tool, I'd suggest fixing your own problems, or 
switching to a non-volunteer supported where people are 
paid to put up with your immature antics.




 [2003-04-26 17:03 UTC] gschlossnagle@php.net
p.s. this syntax works in php5.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 07 10:01:32 2024 UTC