News for hackers. Stuff that matters.
Re:PHP doesn't need 'finally' by hackermom on Friday December 10, 2010 @07:38AM (#1991443)
Syntactic sugar would be the right term for this, I guess. I think everyone who has dealt in languages not having Try-Finally can recall a case or two where it would've felt smooth and tidy to have access to Finally - but never, ever a case where they absolutely required it. No one is having problems in C/++ because of this. No one is having problems in PHP, either.

And saying this will probably get me the flood of down votes, but this has always struck me as being something that is a language "requirement" only in people who have an itch in a hard-to-reach place in the engineering department of their brain - no offense meant. They need it for their sense of order, not for their software.

Re:PHP doesn't need 'finally' by Nitramp on Friday December 10, 2010 @07:57AM (#1991491)
Finally makes it much more easy, and much more readable, to do the right thing. Not having finally means duplicated code and harder to follow methods where resources have to be managed, and for most developers, this simply means resources are not managed properly at all.

C++ doesn't need finally because it has deterministic destructors, which are used to emulate finally clauses. In my opinion, destructors can cause less readable/predictable code than a proper finally block, but that might be matter of taste.

Comment from the language maintainer: The only difference is the second example does rethrow the exception. Though this is still possible (however much more to type) it is wrong design. Since obviously you are using the exceptions as control flow.

This seems quite strange. Finally is a construct to manage control flow, and it's essential to control flow during error conditions. He seems to prefer just swallowing a (potentially critical) exception. Either I'm misreading him, or I think he fails to understand the point of either finally, or exception handling in general.

Re:PHP doesn't need 'finally' by kree10 on Friday December 10, 2010 @10:42AM (#1991946)
"Finally makes it much more easy, and much more readable, to do the right thing."

I think this gets to the heart of what feels 'off' about PHP to me: this kind of passive-aggressiveness against "doing the right thing" seems built-in. Kind of like how in order to avoid XSS, PHP offers htmlspecialchars(), or to avoid SQL injection, mysql_real_escape_string() (I'm aware of PDO, but it seems few if any major projects use it).

Re:PHP doesn't need 'finally' by wvenable on Friday December 10, 2010 @11:26AM (#1992117)
> C++ doesn't need finally because it has deterministic destructors

PHP has deterministic destructors as well. Using them is how I get around the lack of finally -- destructors do the clean up.

Re:PHP doesn't need 'finally' by TimJYoung on Friday December 10, 2010 @02:02PM (#1992721)
Question: when are PHP's object destructors called ? I can see some issues with external resources if an object instance hangs around until it is garbage collected. PHP might not have an issue with this if it does GC for every page load, but other languages like the .NET languages can experience serious issues if you don't release external resources using a finally block, and rather rely on the destruction of the object to release them. In such a case, the destruction may take place a long time after the initial acquisition of the external resources.

My point is that object lifetime <> external resource usage duration in all cases.

Re:PHP doesn't need 'finally' by wvenable on Friday December 10, 2010 @03:59PM (#1993150)
> when are PHP's object destructors called ?

As soon as there are no more references to it. PHP is reference counted (with cycle detection). As soon as the variable containing an object goes out of scope, the destructor will be called.

The fact that Java/C# destructors aren't called immediately (and may not be called at all) is the primary reason that Java has the finally block (C++ doesn't) and why C# also has the using statement.

Re:PHP doesn't need 'finally' by TimJYoung on Friday December 10, 2010 @08:35PM (#1993873)
Thanks for the response. I knew that PHP was reference counted, but what I was asking (not particularly well) was more along the lines of: is it possible to have objects live longer than the scope of a single script ? IOW, is there a point at which the PHP runtime decides that it is going to collect/reclaim everything, similar to a process termination in the OS ? Or is it literally possible to implicitly allocate objects in a global fashion and have them stick around until the PHP runtime itself is terminated ? Thanks again.
Re:PHP doesn't need 'finally' by wvenable on Saturday December 11, 2010 @01:00AM (#1994315)
When a single script terminates, all the memory is deallocated. Basically, as you said, it's just like process termination in the OS.
Re:PHP doesn't need 'finally' by dan_netwalker on Tuesday December 14, 2010 @01:25PM (#2005135)
Well, in fact you could serialize/unserialize the objects and store them somewhere, making use of the "magic" functions __sleep() and __wakeup(), if I recall correctly. But yes, as soon as the web server ends the response or the script gets to the end, it's finished
Re:PHP doesn't need 'finally' by TimJYoung on Saturday December 11, 2010 @12:07PM (#1995160)
Thanks. That's what I thought, but I'm fairly new to PHP, and wasn't entirely sure.
Re:PHP doesn't need 'finally' by randombit on Friday December 10, 2010 @09:23AM (#1991690)
> No one is having problems in C/++ because of this.

No one is having problems with this in C because C doesn't have exceptions.

No one is having problems with this in C++ because C++ supports RAII via destructors which are guaranteed to run on stack unwind.

Re:PHP doesn't need 'finally' by wvenable on Friday December 10, 2010 @04:02PM (#1993168)
You can do RAII in PHP via destructors as well. That's how I "get around" this problem.