Edward Tanguay
Edward Tanguay

Reputation: 193352

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

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);
  }
} 

Upvotes: 42

Views: 32093

Answers (5)

Horace
Horace

Reputation: 161

php >= 7.0

php can catch 'class not found' as Throwable

try {
        return new $className($smartFormIdCode);
} catch (\Throwable $ex) {
        return new SmartFormNull($smartformIdCode);
}

Upvotes: 10

ChadSikorra
ChadSikorra

Reputation: 2869

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);
}

Upvotes: 34

dev-null-dweller
dev-null-dweller

Reputation: 29482

Because php emits fatal error when you ty to create new object of non existing class. To make it work you will need php >= 5.3 and autoload function, where you should try to look for file with class definition or throw your custom exception.

Upvotes: 0

John Parker
John Parker

Reputation: 54445

You need to use class_exists to see if the class exists before you try and instantiate it.

Incidentally, if you're using a class autoloader, be sure to set the second arg to true.

Upvotes: 5

Mchl
Mchl

Reputation: 62395

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.

Upvotes: 51

Related Questions