posted by kevin on April 14, 2009

Simple Example of Problem Code

This is a little brainfart by the developer, instead of getLoop, he meant to just return the value of $this->loop, but instead the method just recursively calls itself.

<?php
echo "First thing on the screen, will it be outputted?";
class Loop {
 
   private $loop;
 
   public function __construct($loop) {
      $this->loop = $loop;
   }
 
   public function getLoop() {
      return $this->getLoop();
   }
}
 
$LoopInstance = new Loop(34);
echo "Loop Value Is ". $LoopInstance->getLoop();
?>

Result

The result is the code will never finish and as a seasoned PHP developer you would probably expect one of a few things to happen.

  • The script reaches max-execution time and triggers a fatal error
  • The script ends with a fatal too-much-recursion or infinite loop error.
  • A Warning of some kind
  • At least a notice!
  • If no parse error you'd expect the text at the very top (First Thing On the Screen...) to at least appear.

But none of those things happen. Just a blank screen, no errors of any kind, and the script immediately ends. That's what makes this so hard to diagnose immediately because there are no errors pointing you in the right direction at all, especially when your application is quite large and then it just doesn't work.

I reported this as a bug, however it got rejected. I was just really surprised by that. PHP is exiting the process, however throwing no errors?

Bookmark and Share

Leave a Comment

Your email address will not be published.

(You can enclose code in <php></php> blocks.)

You may use Markdown syntax.

Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.