As said in my comment above you cannot somehow limit a global exception handler to specific exception types or events.
You can however use phps magical __call()
method to achieve something similar without having to use normal try...catch
blocks in all class methods. Consider this simple example:
<?php
class myException extends Exception {}
class myClass
{
public function __call($funcName, $funcArgs)
{
try {
if (method_exists($this, '_'.$funcName)) {
$this->_myFunction($funcArgs);
}
} catch (myException $e) {
var_dump($e->getMessage());
}
}
public function _myFunction($args)
{
throw new myException('Ooops');
}
}
$myObj = new myClass;
$myObj->myFunction();
The output here obviously will be:
string(5) "Ooops"
There are three things to point out here:
- "normal" methods like
_myFunction()
do not have to implement a try...catch
block
- caught exceptions can be limited to certain types and can be handled inside the object scope
- no global exception handler is registered, so no global exceptions are caught.
A huge disadvantage with such setup is however, that IDEs fail to support such method chains, so you get no auto completion and no signature help for object methods.