PHP foreach循环奇数引用行为


PHP foreach loop odd references behaviour

我有一个foreach循环和引用的奇怪问题。这是我的代码:

  $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>";                                                                                                                                                                                                                                                               
  }                   

因此,如果我们假设这两个对象都是在数据库中创建的,那么显示的输出是:

1-1
2-2

你猜我的问题是:为什么$authors[0]->id指的是$author->id??我想这是引用的问题,但由于我在foreach循环中不使用引用,我不知道它来自哪里!

欢迎提出任何建议。感谢

为什么$authors[0]->id指的是$author->id??

它没有(在第一次迭代之后)。

其他地方有问题(可能在Author::__constructAuthor::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
*/