0

I have a weird issue with a foreach loop and references. Here is my code:

  $authors = array(                                                                                                                                                            
       new Author(array('first_name'=>'Name 1','last_name'=>'last name 1')),                                                                                                         
       new Author(array('first_name'=>'name 1','last_name'=>'last name 2')),                                                                                                   
  );                                                                                                                                                                           

  foreach($authors as $key => $author){                                                                                                                                                
    $authors[$key] = Author::manager()->getOrCreate($author);                                                                                                                       
    print $author->id."-".$authors[0]->id."<br>";                                                                                                                                                                                                                                                               
  }                   

So if we assume that both of those objects are created in the database, then the output shown is :

1-1
2-2

As you guess my question is: why does $authors[0]->id refers to $author->id ?? I suppose that it is a problem with reference but since I don't use reference in the foreach loop, I have no idea where it comes from!

Any suggestion will be welcome. Thanks

3
  • Are you sure the output isn't 1-1, 2-1? Looks like that'd be what it'd output. Just wanted to confirm? Commented Mar 13, 2012 at 19:06
  • Since you don't store any id's when you create the $authors array they will be set to 0, 1, .... So $authors[0] would be the same as $author in the first iteration. Commented Mar 13, 2012 at 19:08
  • @Ben No the output is definitely 1-1 2-2. Commented Mar 13, 2012 at 19:15

1 Answer 1

1

why does $authors[0]->id refers to $author->id ??

It doesn't (after the first iteration).

There's something wrong elsewhere (perhaps in Author::__construct or Author::manager):

class Author
{
    public $id;

    function __construct($params)
    {
        $this->id = substr($params['last_name'], -1);
    }
}


$authors = array(                                                                                                                                                            
    new Author(array('first_name'=>'Name 1','last_name'=>'last name 1')),                                                                                                         
    new Author(array('first_name'=>'name 1','last_name'=>'last name 2')),                                                                                                   
);                                                                                                                                                                           

foreach($authors as $key => $author){                                                                                                                                                                                                                                                                      
    print $author->id."-".$authors[0]->id."<br>";                                                                                                                                                                                                                                                               
}

/* 
output:

1-1
2-1

*/
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed, there is an ORM layer which execute the queries to create or get the data. Then I retrieved the PHP object model fed with the shadow data such as the id attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.