Re: [PATCH] [RFC] Closures and lambda functions in PHP

From: Date: Fri, 20 Jun 2008 14:46:40 +0000
Subject: Re: [PATCH] [RFC] Closures and lambda functions in PHP
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-38469@lists.php.net to get a copy of this message
Hi,
I would be fine with requiring an explicit declaration of $this if it helped avoid memory leaks.
I would propose to always require explicit declaration of $this, even if there is no memory-leak problem. This would make it easier to distinguish plain lambdas from closures and would prevent closures from being created by accident.
I believe that is not neessary: $this is not a normal variable in PHP (see for example <http://bugs.php.net/bug.php?id=33652> or <http://bugs.php.net/bug.php?id=43163>) but a language construct. So it is possible to determine if this is used at compile time (if $this is used inside the function, op_array->this_var != -1 if I'm not mistaken) - and therefore automatically importing it (it is automatically available in local scope of class methods anyway). The only possible clash could be nested lambdas - but there you could simply enforce the creation of $this in all functions containing a lambda which uses $this). To sum it up: I believe it is possible to optimize it that way that $this is only stored within the closure if the closure actually needs it - but without the need for declaring it explicitly. Regards, Christian

« previous php.internals (#38469) next »