Last active
October 28, 2018 20:34
-
-
Save eliran-arkabi/86c0ab4fbe886fee2a0b565cef7fa81b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** Warning if use this trait on class has be use magic methods __call | __classStatic dont use it | |
| ** Just copy the code to your magic methods on the class | |
| * To fire the call need to and end Try | |
| * example if you class Foo::test() to fire need to call Foo::testTryCatch() | |
| * example if you class $ob->test() to fire need to call $ob->testTryCatch() | |
| */ | |
| trait TryCatchTrait{ | |
| public function __call($method, $arguments) | |
| { | |
| if (preg_match('/TryCatch$/', $method)){ | |
| //replace TryCatch | |
| $method = str_replace('TryCatch', '', $method); | |
| //try to call the method | |
| try { | |
| return call_user_func_array([$this, $method], $arguments); | |
| } catch (\Exception $e) { | |
| // catch all | |
| $message = 'Exception catch on call Method: ' . $method . ' Class: ' . __CLASS__ ; | |
| self::handleExceptions($e, $message); | |
| } | |
| } | |
| return parent::__call($method, $arguments); | |
| } | |
| public static function __callStatic($method, $arguments) | |
| { | |
| if (preg_match('/TryCatch$/', $method)){ | |
| //replace TryCatch | |
| $method = str_replace('TryCatch', '', $method); | |
| //try to call the method | |
| try { | |
| return call_user_func_array(__CLASS__ . "::$method", $arguments); | |
| } catch (\Exception $e) { | |
| // catch all | |
| $message = 'Exception catch on call static Method: '. __CLASS__ . '::' . $method; | |
| self::handleExceptions($e, $message); | |
| } | |
| } | |
| return parent::__callStatic($method, $arguments); | |
| } | |
| // over write this method on the class use this trait | |
| private static function handleExceptions(\Exception $e, string $message) | |
| { | |
| echo $message; | |
| echo "\nMessage:" . $e->getMessage() . "\n"; | |
| } | |
| } | |
| /* @@ Just for test remove this class on use @@ */ | |
| class Foo{ | |
| use TryCatchTrait; | |
| public static function test($throw) | |
| { | |
| if ($throw){ | |
| throw new \Exception('Some Exception'); | |
| } | |
| echo "test\n"; | |
| } | |
| public function testOb($throw) | |
| { | |
| if ($throw){ | |
| throw new \Exception('Some Exception'); | |
| } | |
| echo "test\n"; | |
| } | |
| //custom handle | |
| private static function handleExceptions(\Exception $e, string $message) | |
| { | |
| echo "handleExceptions class\n $message"; | |
| } | |
| } | |
| //object | |
| $f = new Foo; | |
| $f->testObTryCatch(false); // echo test | |
| $f->testObTryCatch(true);// catch Exception | |
| //static | |
| Foo::testTryCatch(false); //echo test | |
| Foo::testTryCatch(true); // catch Exception |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment