php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22981 Problem with "=& new" and locally created instances
Submitted: 2003-03-31 08:43 UTC Modified: 2003-08-14 01:19 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: Jens dot Pilgrim at xx-well dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.3.0 OS: Windows 2000
Private report: No CVE-ID: None
 [2003-03-31 08:43 UTC] Jens dot Pilgrim at xx-well dot com
Hi,

I've got a problem with the "=& new" construct. A code sample tells more
than 1000 words:

<?php

class foo {}

$g_fooInstance = null;

class factory {
  function factory() {
    global $g_fooInstance;
    if (is_null($g_fooInstance)) {
      $g_fooInstance =& new foo();   // <-- here's the problem
//      $g_fooInstance = new foo();  // <-- this works
    }
  }
}

function main() {
  $theFactory =& new factory();
  global $g_fooInstance;
  if (is_null($g_fooInstance)) {
    echo ('This isn\'t expected - why is it null?');
  } else {
    echo ('It works as expected');
  }
}

// run
main();
?>

This code is running like that and demonstrates exactly the "weird" behaviour.
Running this example, the variable $g_fooInstance is null in function main(). But why? Is the reference to the constructed object descructed after leaving the scope of function factory::factory()? Or in other words, are locally created instances always destructed, regardless if there are other references on the instance?

I've posted this bug as a question in the general mailing list (on Fr 03/28/2003), and others tested and reproduced this bug too...

Regards,

jens


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-05-05 15:18 UTC] moriyoshi@php.net
related to bug #22231
 [2003-08-14 01:19 UTC] sniper@php.net
Fixed in PHP 5.

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Dec 04 08:00:01 2025 UTC