Try-Catch-Finally in PHP
2012-08-29 00:00
246 查看
As of PHP 5.3 web developers can use a wonderful feature of the language, the
Some say the need for a
Using
In this situation rethinking the design can help avoid the need for
But sometimes life’s not that easy. Sometimes your hands are bound, sometimes it’s not that trivial to circumvent the need for
Note that this workaround does not handle the
__invoke()magic method. This enables PHP to provide the Closure class and based on it the ability to define anonymous functions. Anonymous function is a powerful language construct, it can come handy in a number of ways. For example, it makes the code easier to manage by letting you define locally visible small functions inside a method or a function. This is useful when you want to sort an array by some special property of the elements in it – you don’t have to define a named function or method in your global scope or in a class, you can just hide the comparison callback inside the method that needs a specially sorted array. But anonymous functions combined with other language constructs, can even be used to emulate some features missing from PHP:
finallyfor instance.
Some say the need for a
finallykeyword is the result of bad design. Most people would use
finallyin a similar situation like this to avoid code duplication in the
catchblock and after the
try-
catchstatement:
class A { public function doSomething() { $my_resource = open_my_resource(); try { $result = use_my_resource($my_resource); } catch (Exception $e) { free_my_resource($my_resource); throw $e; } free_my_resource($my_resource); return $result; } }
Using
finallythe above code snippet would look like this:
class A { public function doSomething() { $my_resource = open_my_resource(); try { $result = use_my_resource($my_resource); } finally { free_my_resource($my_resource); } return $result; } }
In this situation rethinking the design can help avoid the need for
finally:
class MyResource { public function __construct() { $this->resource = open_my_resource(); } public function __destruct() { free_my_resource($this->resource); } public function use() { return use_my_resource($this->resource); } private $resource; } class A { public function doSomething() { $my_resource = new MyResource(); $result = $my_resource->use(); return $result; } }
But sometimes life’s not that easy. Sometimes your hands are bound, sometimes it’s not that trivial to circumvent the need for
finally. That is the case when a nice hack based on closures can help:
class A { public function doSomething() { $my_resource = open_my_resource(); $finally = finally(function () use ($my_resource) { free_my_resource($my_resource); }); try { $result = use_my_resource($my_resource); } catch (Exception $e) { fprintf(STDERR, "Error: %s\n", $e->getMessage()); throw $e; } $finally(); return $result; } }
Note that this workaround does not handle the
return-inside-
finallyproblem at all as you cannot return from the function from the simulated
finallyblock
相关文章推荐
- using try catch finally in SQL
- PHP中PDO连接进行事物回滚及try{}catch(){}
- Javascript try catch finally
- try catch finally 与 return 的问题
- 今天开始学Java try-catch-finally
- 简单看try,catch,finally
- 编译错误:Try没有catch或者Finally
- Java中的try-catch-finally-return的执行顺序
- Java异常处理机制 try-catch-finally
- PHP中try{}和catch{}的具体用法详细解释
- java中关于try、catch、finally中的细节分析
- Using TRY/CATCH to Resolve a Deadlock in SQL Server 2005
- java关于异常处理只使用try-finally而不使用catch
- Java中的try,catch,finally
- Try,Catch,Finally三块中如果有Return是怎么个运行顺序
- 有return的情况下try catch finally的执行顺序(最有说服力的总结)
- 有return的情况下try catch finally的执行顺序
- Java中的try catch finally
- try catch finally的执行顺序即结果
- try_catch_finally的注意事项