We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

\Phalcon\Loader handling "PHP Fatal error: Uncaught Error: Class ABC not found"

The system I am working on is highly modular and there is the possibility of modules that are added by 3rd parties... therefore I would like to add more intellegent error handling than just the default (and fatal) HTTP 500 error.

I would like to handle/intercept a "Class not found" error by throwing an exception instead. Obviously errors like this should be caught during development, but throwing an exception instead of a fatal error would at least allow a recovery pattern on a production system.

I have looked into the Event Manager for \Phalcon\Loader and I see that there are 3 "loader" events that I can work with (beforeCheckClass, pathFound, afterCheckClass)... but there doesn't appear to be the critical one... pathNotFound!!

For example:

// works... fires all 3 events
$widget = new \Cool\Widget();  

// non-existant class... fires only "beforeCheckClass" and "afterCheckClass" events
try {
    $fake = new \Cool\Fake(); 
} catch (\Exception $e) {
    die('<pre>' . $e->__toString() . '</pre>');   // never called
}

What are my options?

According to documentation the Loader has event beforeCheckPath.

What documentation are you referring to? In the user guide for autoloader, I only see the 3 events I mentioned above.

The class documentation and the devtools stubs do not list any events.

I also updated the original code to include the fact that the \Phalcon\Loader\Exception is not being called either.

It seems like the only way for me to implement this behavior is to implement my own autoloader (which voids the advantages of the \Phalcon\Loader).

I am running Phalcon 3.0.1 with PHP 7.0.12 in a MAMP environment.