On 15/03/2020 18:17, tyson andre wrote:
If this feature freed variables when going out of scope,
it would be compiled more like the following (if there were multiple lets):

JavaScript doesn't have destructors, but PHP does, which makes the 
implementation a tiny bit more complex.

```
try {
     $x = new Foo();
     $otherLet = new Other();
     $x->bar();
} finally {
     // try/finally is useful if unset($otherLet) could throw from __destruct
     try {
         unset($otherLet);
     } finally {
         unset($x);
     }
}
somethingElse();
```


I'm still not 100% clear why all this would be necessary. Do you know how the equivalent code works with function-scoped variables? As far as I can see, returning from a function will successfully call multiple destructors even if one of them throws an exception. Could exiting block scope use that same algorithm?

Having the variable become inaccessible but not actually deallocated seems like it would cause a lot of confusion.

For instance:

{
    let $fh1 = fopen('/tmp/foo', 'wb');
    flock($fh1);
    fwrite($fh1, 'Hello World');
    // no fclose(), but $fh1 has fallen out of scope, which would normally close it
}
{
    let $fh2 = fopen('/tmp/foo', 'wb');
    flock($fh2, LOCK_EX); // won't obtain lock, because $fh1 is still open, but no longer accessible
}

Regards,

--
Rowan Tommins (né Collins)
[IMSoP]

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

Reply via email to