title: Why doesn't PHP catch a "Class not found" error?
body: <p>In the following example, if the class does not exist, I want to catch the error and create a <code>Null</code> class instead.</p>
  
  <p>But in spite of my try/catch statements, PHP simply tells me <code>Class 'SmartFormasdfasdf' not found</code>.</p>
  
  <p><strong>How can I get PHP to catch the 'class not found' error?</strong></p>
  
  <pre><code>&lt;?php
  class SmartFormLogin extends SmartForm {
      public function render() {
          echo '&lt;p&gt;this is the login form&lt;/p&gt;';
      }
  }
  
  class SmartFormCodeWrapper extends SmartForm {
      public function render() {
          echo '&lt;p&gt;this is the code wrapper form&lt;/p&gt;';
      }
  }
  
  class SmartFormNull extends SmartForm {
      public function render() {
          echo '&lt;p&gt;the form "' . htmlentities($this-&gt;idCode) . '" does not exist&lt;/p&gt;';
      }
  }
  
  class SmartForm {
  
      protected $idCode;
  
      public function __construct($idCode) {
          $this-&gt;idCode = $idCode;
      }
  
      public static function create($smartFormIdCode) {
          $className = 'SmartForm' . $smartFormIdCode;
          try {
              return new $className($smartFormIdCode);
          } catch (Exception $ex) {
              return new SmartFormNull($smartformIdCode);
          }
      }
  }
  
  $formLogin = SmartForm::create('Login');
  $formLogin-&gt;render();
  $formLogin = SmartForm::create('CodeWrapper');
  $formLogin-&gt;render();
  $formLogin = SmartForm::create('asdfasdf');
  $formLogin-&gt;render();
  ?&gt;
  </code></pre>
  
  <h1>Solution:</h1>
  
  <p>Thanks @Mchl, this is how I solved it then:</p>
  
  <pre><code>public static function create($smartFormIdCode) {
    $className = 'SmartForm' . $smartFormIdCode;
    if(class_exists($className)) {
      return new $className($smartFormIdCode);
    } else {
      return new SmartFormNull($smartFormIdCode);
    }
  } 
  </code></pre>