Re: Problem with "=& new" and locally created instances
| From: | Jason k Larson | Date: | Fri, 28 Mar 2003 12:34:02 +0000 |
| Subject: | Re: Problem with "=& new" and locally created instances | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-141428@lists.php.net to get a copy of this message | ||
Maybe I'm missing something here ... but what is the point of trying to create a reference to a new object ... it hasn't been ceated prior to make reference to. Can't you simply create the new object and then make whatever (if needed) references?
--
Jason k Larson
Jens von Pilgrim wrote:
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? Is this a bug (the reference mechanism isn't working as expected) - or a feature (locally created instances are immediately after leaving the scope)? Maybe I've programmed Java too long... Regards, jens