Results 1 to 5 of 5

Thread: PHP4: Iterate object by reference

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Resolved 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.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    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.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    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.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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
  •  



Click Here to Expand Forum to Full Width