RE: [PHP-QA] RE: [PHP-DEV] Re: [PHP-QA] $obj = new foo() patch

From: Date: Sat, 11 Nov 2000 01:09:22 +0000
Subject: RE: [PHP-QA] RE: [PHP-DEV] Re: [PHP-QA] $obj = new foo() patch
References: 1  Groups: php.dev php.qa 
Request: Send a blank email to php-dev+get-37720@lists.php.net to get a copy of this message
Kirill and others, > Hmm. I thought that > > $GLOBALS['a'] = new A; > > is absolute equivalent to > > global $a; > $a = new A; > > I don't care in this example what is returned by operator 'new'. > I suppose, that $a in second case should be absolutely equivalent to > GLOBALS['a'] regardsless right part of '=' sign. I though that too but apparantly what happens is $a is created inside the context of the function and points to the data of the real global $a so if you do something like $a = &$foo inside the function the real $a's reference won't change. This should probably be documented better since saying something "PHP doesn't support C like pointers" doesn't quite cut it. It took me quite some time (and a number of bug postings) to figure this out. > Anyway, I don't understand why Andi's patch can work in both cases. > OK, 'new' returns reference. But $a should be === $GLOBALS['a'], IMO. > Or I've missed something? Explained above why not. I fully agree that as that is what is expected but after looking under the hood of PHP a bit I can see why it isn't. Of course if I had it my way I'd just get rid of the whole global $foo thing all together. :) > If we want to force 'rich' OO design, let's wait until PHP 5.0. IMO, > a few peoply will upgrade to 4.0.4 if they'll have to rewrite their code > (possibly, a LOT of code). > But, I think, core team understands importance of compatibility issue. That's > why Andi reversed the patch for now, isn't it? ;-) Well I have to agree with you there. There are a lot of OO features I would love to see in PHP but I'm certainly not counting on them until future versions. The problem (regarding Andi's patch) is that the expected behavior of: <?php class foo { function foo($a) { global $ListOfObjects; $this->a = a; $ListOfObjects[] = &$this; } $bar = new foo() ?> would be that $ListOfObjects[0] and $bar referenced the same data. They do not. This is very frustrating to many people are there are 5 or more bugs in the bugs database pointing this out. I'm convinced this is a serious issue and needs to be fixed. I can only pray that *some* fix will get into PHP 4.0.4. (BTW -- regarding my explanation above concerning the difference between global $foo and $GLOBALS['foo']. This example will still work since I'm not changing the reference to $ListOfObjects just its array members.) Regards, Mike

« previous php.dev (#37720) next »