On Tue, 2012-07-24 at 14:50 +0100, Andrew Faulds wrote:
> > As PHP has destructors there is less need for "finally" compared to
> > other languages. What are the cases where an extra language construct is
> > needed? (i.e. one can also use C++-like RAII things ...)
> >
> > The RFC is also missing to demonstrate the order of finally calls in
> > nested try-catch-blocks.
> >
> >
> Tempfiles come to mind. Also, yes, PHP has destructors, but you are not 
> always dealing with custom-made objects to handle this. And you may want 
> something to happen before GC.

finally is no sufficient way to ensure tempfiles will be delete. Best
approach for that is to delete them after opening (but keep the
handle(s) open) so the OS will guarantee they are deleted. finally might
not be called (in the proposed implementation it isn't called after an
die()/exit() or a fatal error)

When not dealing with custom objects you might wrap them in a
RAII-Container. PHPs GC is mostly refcount-based and well defined. If
you keep a single reference to a RAII-container or something it is well
defined when it will be called. finally rules (especially with nested
try blocks) is an extra thing to be learned.

johannes


P.S. simple, untested, ad hoc example of a quite generic raii container:

   class RAIIContainer {
       private $cb;
       public function __construct(callable $cb) {
           $this->cb = $cb;
       }
       public function __destruct() {
           $cb = $this->cb; $cb();
       }
   }

   try {
       $file = fopen(....);
       $rc = new RAIIContainer(
          function() use ($file) { if ($file) { unlink($file); }});

       /* do something /
   } catch (SomeException $e) {
   }

here unlink will even be called at an exit() or something, certainly can
be made nice ... and yes an extra syntax might be "nicer" but is it
common enough to excuse making the language larger?


-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to