3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class UserService { public function userFactory() { $user=new User(); $user->id='test_id'; return $user; } } class Conversation { public $participants=array(); public function getParticipants() { return $this->participants; } } class User { public $id; } $u1=new User(); $u2=new User(); $conversation=new Conversation(); $conversation->participants[]=$u1; $conversation->participants[]=$u2; $userService=new UserService(); foreach($conversation->getParticipants() as &$participant) { $participant=$userService->userFactory(); } print_r($conversation);
based on 0UMZI
Output for 5.5.18 - 5.5.38, 5.6.3 - 7.2.0
Conversation Object ( [participants] => Array ( [0] => User Object ( [id] => ) [1] => User Object ( [id] => ) ) )
Output for 5.5.0 - 5.5.16, 5.6.0 - 5.6.2
Conversation Object ( [participants] => Array ( [0] => User Object ( [id] => test_id ) [1] => User Object ( [id] => test_id ) ) )