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(); ?>
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); } }
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 ) { // ... }
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.
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.
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.
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.
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With