title: Intermittent error 'Trying to get property of non-object'
body: <p>We are getting a PHP notice on our production server:</p>
  
  <pre><code>[PHP Notice] Trying to get property of non-object
  </code></pre>
  
  <p>In our code we have two arrays of <code>Foo</code> objects each with a <code>pos</code> member variable:</p>
  
  <pre><code>$locked_foos
  $unlocked_foos
  </code></pre>
  
  <p>I'm running a while loop to order and merge these <code>Foo</code>s into a new array result:</p>
  
  <pre><code>$cur_pos = 1;
  $result  = [];
  
  // Extract first locked and unlocked foos
  $locked_foo = reset($locked_foos);
  $unlocked_foo = reset($unlocked_foos);
  
  // While there is either an unlocked or locked foo..
  while($locked_foo || $unlocked_foo){
  
    // If there is no unlocked (thus must be a locked) or the locked's position is current..
    if(!$unlocked_foo || $locked_foo-&gt;pos == $cur_pos){ // **Error**
  
      // Use locked, set pos and iterate locked
      $locked_foo-&gt;pos = $cur_pos;
      $result[] = $locked_foo;
  
      $locked_foo = next($locked_foos);
    }
    // Else..
    else {
  
      // Use unlocked, set pos and iterate unlocked
      $unlocked_foo-&gt;pos = $cur_pos;
      $result[] = $unlocked_foo;
  
      $unlocked_foo = next($unlocked_foos);
    }
  
    // Iterate current position
    $cur_pos++;
  }
  </code></pre>
  
  <p>The error occurs at the line shown.</p>
  
  <p>I assume the error occurs when one of the <code>$locked_foos</code> is set to something other than an object, so I added the lines:</p>
  
  <pre><code>  if($locked_foo &amp;&amp; !is_object($locked_foo)){
      throw new Exception('FOOOO!');
    }
  </code></pre>
  
  <p>Just inside the while loop.</p>
  
  <p>I'm still getting the same error and no exception is thrown, how is this possible?</p>