# Odd reference => global bug?

- Thread: https://externals.io/message/13138
- Original archive: https://news-web.php.net/php.internals/13138
- Started by Bob Glamm on 2004-10-04T14:39:16+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #13138 — Bob Glamm — 2004-10-04T14:39:16+00:00

URL: https://externals.io/message/13138#13138

I saw this behavior this morning and was curious if I'd tripped a bug
in PHP (running version 4.3.7 at the moment).  I scanned through the
bug list for this particular bug but didn't find it.  I'm running it
by you all because I'm not intimately familiar with the reference
system to really be sure that it's a bug and not just my misunderstanding.

A demonstration script is included below:
globals $x and $v are set to NULL.  q() is called
and references globals $x and $v.  $x is set to the new class X;
$v is set to a reference to a new class Y (by means of a factory
function in X, a common structure in PEAR).

My version of PHP prints `NULL` at the `var_dump()` statement immediately
following the call to q(); I would expect it to dump an instance of
class Y.  So: am I misunderstanding references (and if so, why is the
behavior what it is), is this a duplicate
of another known bug, is this bug fixed in a newer version of PHP,
or should I file a new bug?

-Bob

<?php

class Y		// class created by factory in X, below
{
  function Y($t) { $this->a = $t; }
}

class X
{
  function X() { }

  function &getY($t)	// factory method to create Y and return a ref to it
  {
    $k = new Y($t);
    return($k);
  }
}

$x = null;
$v = null;

function q()
{
  global $x, $v;

  $x = new X();		// get an X simply to acquire a Y
  $v =& $x->getY("here");	// use the factory in X to assign a ref
  				// to Y to the global $v
}

q();			// initialize the globals with X and &Y
var_dump($v);		// should dump a Y, instead prints `NULL` ?

?

---

## #13139 — Bob Glamm — 2004-10-04T14:49:11+00:00

URL: https://externals.io/message/13138#13139 · In reply to: #13138

FWIW, after a bit more investigating, changing the call in q() to the
factory method getY() from:

  $v =& $x->getY("here");

to

  $GLOBALS['v'] =& $x->getY("here");

the var_dump($v) in the global section of code works as expected.

-Bob

> I saw this behavior this morning and was curious if I'd tripped a bug
> in PHP (running version 4.3.7 at the moment).  I scanned through the
> bug list for this particular bug but didn't find it.  I'm running it
> by you all because I'm not intimately familiar with the reference
> system to really be sure that it's a bug and not just my misunderstanding.
> 
> A demonstration script is included below:
> globals $x and $v are set to NULL.  q() is called
> and references globals $x and $v.  $x is set to the new class X;
> $v is set to a reference to a new class Y (by means of a factory
> function in X, a common structure in PEAR).
> 
> My version of PHP prints `NULL` at the `var_dump()` statement immediately
> following the call to q(); I would expect it to dump an instance of
> class Y.  So: am I misunderstanding references (and if so, why is the
> behavior what it is), is this a duplicate
> of another known bug, is this bug fixed in a newer version of PHP,
> or should I file a new bug?
> 
> -Bob
> 
> <?php
> 
> class Y		// class created by factory in X, below
> {
>   function Y($t) { $this->a = $t; }
> }
> 
> class X
> {
>   function X() { }
> 
>   function &getY($t)	// factory method to create Y and return a ref to it
>   {
>     $k = new Y($t);
>     return($k);
>   }
> }
> 
> $x = null;
> $v = null;
> 
> function q()
> {
>   global $x, $v;
> 
>   $x = new X();		// get an X simply to acquire a Y
>   $v =& $x->getY("here");	// use the factory in X to assign a ref
>   				// to Y to the global $v
> }
> 
> q();			// initialize the globals with X and &Y
> var_dump($v);		// should dump a Y, instead prints `NULL` ?
> 
> ?

---

## #13140 — messju mohr — 2004-10-04T14:52:03+00:00

URL: https://externals.io/message/13138#13140 · In reply to: #13138

> I saw this behavior this morning and was curious if I'd tripped a bug
> in PHP (running version 4.3.7 at the moment).  I scanned through the
> bug list for this particular bug but didn't find it.  I'm running it
> by you all because I'm not intimately familiar with the reference
> system to really be sure that it's a bug and not just my misunderstanding.
> 
> A demonstration script is included below:
> globals $x and $v are set to NULL.  q() is called
> and references globals $x and $v.  $x is set to the new class X;
> $v is set to a reference to a new class Y (by means of a factory
> function in X, a common structure in PEAR).
> 
> My version of PHP prints `NULL` at the `var_dump()` statement immediately
> following the call to q(); I would expect it to dump an instance of
> class Y.  So: am I misunderstanding references (and if so, why is the
> behavior what it is), is this a duplicate
> of another known bug, is this bug fixed in a newer version of PHP,
> or should I file a new bug?
> 
> -Bob
> 
> <?php
> 
> class Y		// class created by factory in X, below
> {
>   function Y($t) { $this->a = $t; }
> }
> 
> class X
> {
>   function X() { }
> 
>   function &getY($t)	// factory method to create Y and return a ref to it
>   {
>     $k = new Y($t);
>     return($k);
>   }
> }
> 
> $x = null;
> $v = null;
> 
> function q()
> {
>   global $x, $v;

this makes a reference just like
$v =& $GLOBALS['v'];
would
 
>   $x = new X();		// get an X simply to acquire a Y
>   $v =& $x->getY("here");	// use the factory in X to assign a ref
>   				// to Y to the global $v

this overrides the old reference (the one to $GLOBALS['v']) with a new
(local) one.

this behaviour may look odd, but it is not a bug.

do "$GLOBALS['v'] =& $x->getY("here")" instead and $v will be assinged
as expected.

> }
> 
> q();			// initialize the globals with X and &Y
> var_dump($v);		// should dump a Y, instead prints `NULL` ?
> 
> ?

---

## #13141 — Michael Wallner — 2004-10-04T14:54:30+00:00

URL: https://externals.io/message/13138#13141 · In reply to: #13138

Hi Bob Glamm, you wrote:

[...]

Seems to be the same bug as in:

function singleton()
{
	static $instance;
	if (!isset($instance)) {
		$instance = &new SomeClass;
	}
	return $instance;
}

If you use "&new" notation in your script, 
you'll see that $x will be NULL, too.

Regards,
-- 
Michael - < mike(@)php.net
