Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't PHP catch a "Class not found" error?

Tags:

php

try-catch

In the following example, if the class does not exist, I want to catch the error and create a Null class instead.

But in spite of my try/catch statements, PHP simply tells me Class 'SmartFormasdfasdf' not found.

How can I get PHP to catch the 'class not found' error?

<?php class SmartFormLogin extends SmartForm {     public function render() {         echo '<p>this is the login form</p>';     } }  class SmartFormCodeWrapper extends SmartForm {     public function render() {         echo '<p>this is the code wrapper form</p>';     } }  class SmartFormNull extends SmartForm {     public function render() {         echo '<p>the form "' . htmlentities($this->idCode) . '" does not exist</p>';     } }  class SmartForm {      protected $idCode;      public function __construct($idCode) {         $this->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->render(); $formLogin = SmartForm::create('CodeWrapper'); $formLogin->render(); $formLogin = SmartForm::create('asdfasdf'); $formLogin->render(); ?> 

Solution:

Thanks @Mchl, this is how I solved it then:

public static function create($smartFormIdCode) {   $className = 'SmartForm' . $smartFormIdCode;   if(class_exists($className)) {     return new $className($smartFormIdCode);   } else {     return new SmartFormNull($smartFormIdCode);   } }  
like image 332
Edward Tanguay Avatar asked Dec 12 '10 11:12

Edward Tanguay


People also ask

How can I catch exception in PHP?

Because exceptions are objects, they all extend a built-in Exception class (see Throwing Exceptions in PHP), which means that catching every PHP exception thrown is as simple as type-hinting the global exception object, which is indicated by adding a backslash in front: try { // ... } catch ( \Exception $e ) { // ... }

Can we catch fatal error in PHP?

You can "catch" these "fatal" errors by using set_error_handler() and checking for E_RECOVERABLE_ERROR. I find it useful to throw an Exception when this error is caught, then you can use try/catch.

How can I force error in PHP?

Use trigger_error() after you used set_error_handler() to register your own callback function which either logs or emails the error codes to you, and echo a simple friendly message to the user. trigger_error always reports the line and file that trigger_error was called on.

What is PHP exception class?

An exception is an object that describes an error or unexpected behaviour of a PHP script. Exceptions are thrown by many PHP functions and classes. User defined functions and classes can also throw exceptions. Exceptions are a good way to stop a function when it comes across data that it cannot use.


2 Answers

Because it's a fatal error. Use class_exists() function to check if class exist.

Also: PHP is not Java - unless you redefined default error handler, it will raise errors and not throw exceptions.

like image 99
Mchl Avatar answered Sep 22 '22 14:09

Mchl


Old question, but in PHP7 this is a catchable exception. Though I still think the class_exists($class) is a more explicit way to do it. However, you could do a try/catch block using the new \Throwable exception type:

$className = 'SmartForm' . $smartFormIdCode; try {     return new $className($smartFormIdCode); } catch (\Throwable $ex) {     return new SmartFormNull($smartformIdCode); } 
like image 22
ChadSikorra Avatar answered Sep 20 '22 14:09

ChadSikorra