|
-
Jul 20th, 2007, 03:57 AM
#1
Thread Starter
Fanatic Member
PHP4: Iterate object by reference
I know I know, 4 loses support next year but I couldn't convince my co-developers to migrate from 4 to 5 so I'm stuck; we are stuck. Anyway, I need help with
foreach ($someArray as $someObject) {
// Do Changes to $someObject
}
I can't pass the changes back to the array... Help anyone? Thanks.
Last edited by nebulom; Jul 20th, 2007 at 04:01 AM.
-
Jul 20th, 2007, 04:00 AM
#2
Thread Starter
Fanatic Member
Re: PHP4: Iterate object by reference
Solved it by for ($i = 0; $i < count($someArray); $i++) and $someObject = &$someArray[$i] but can't I do it with foreach? It's more neat that way.
-
Jul 20th, 2007, 09:16 AM
#3
Re: PHP4: Iterate object by reference
It's the same!
PHP Code:
foreach ($c as &$v);
foreach ($c as $k => &$v);
However, PHP 5 passes objects by reference by default anyway, so this syntax is only necessary in PHP 4. The only use for it in PHP 5 is when dealing with collections of primitives, which aren't passed by reference.
-
Jul 22nd, 2007, 07:35 PM
#4
Thread Starter
Fanatic Member
Re: PHP4: Iterate object by reference
Thanks for the reply P. That gives Parse error: parse error, unexpected '&', expecting T_VARIABLE or '$'... on PHP4.4.1. Anyway I can still use the good for loop so it's semi-resolve now.
-
Jul 22nd, 2007, 08:37 PM
#5
Re: PHP4: Iterate object by reference
Oh, that was added in PHP 5. My mistake. Sorry.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|