Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:39249 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 42427 invoked from network); 23 Jul 2008 21:22:38 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Jul 2008 21:22:38 -0000 Authentication-Results: pb1.pair.com header.from=antoniadis@noodles.gr; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=antoniadis@noodles.gr; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain noodles.gr from 209.85.132.246 cause and error) X-PHP-List-Original-Sender: antoniadis@noodles.gr X-Host-Fingerprint: 209.85.132.246 an-out-0708.google.com Received: from [209.85.132.246] ([209.85.132.246:40315] helo=an-out-0708.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id AA/0B-12534-D11A7884 for ; Wed, 23 Jul 2008 17:22:38 -0400 Received: by an-out-0708.google.com with SMTP id c14so523231anc.1 for ; Wed, 23 Jul 2008 14:22:35 -0700 (PDT) Received: by 10.100.8.4 with SMTP id 4mr407183anh.38.1216848155426; Wed, 23 Jul 2008 14:22:35 -0700 (PDT) Received: by 10.100.142.12 with HTTP; Wed, 23 Jul 2008 14:22:35 -0700 (PDT) Message-ID: <641f88250807231422x53d5475t3c665524045b4e6@mail.gmail.com> Date: Thu, 24 Jul 2008 00:22:35 +0300 Sender: antoniadis@noodles.gr To: internals@lists.php.net MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_173853_27396357.1216848155432" X-Google-Sender-Auth: 95aa516220563695 Subject: Php 5.3 Snap - Lambda functions and $this scope From: george@noodles.gr ("George Antoniadis") ------=_Part_173853_27396357.1216848155432 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline This is based on a bug submitted at http://bugs.php.net/bug.php?id=45604 Description: ------------ When creating a normal function inside a class and calling it, the function doesn't have access to $this. "Fatal error: Using $this when not in object context" When creating a lambda function inside a class, $this is visible from the new function's scope and can be accessed normally. Wouldn't it be better (and maybe safer) to allow the use of $this as a closure instead of passing it to the new lambda function? Currently trying to use $this as a closure dies with a "Fatal error: Cannot use $this as lexical variable" error. Example for the suggestion. $x = function () use ($this) { return $this->hello; }; Reproduce code: --------------- > > class something > { > public $hello = 'Hello world!'; > > public function world() > { > $x = function () { return $this->hello; }; > return $x(); > } > } > > $s = new something(); > echo $s->world(); > > ?> Expected result: ---------------- Not to be able to use $this. ------=_Part_173853_27396357.1216848155432--