Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88869 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 32268 invoked from network); 19 Oct 2015 09:19:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Oct 2015 09:19:25 -0000 Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.49 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.215.49 mail-lf0-f49.google.com Received: from [209.85.215.49] ([209.85.215.49:33440] helo=mail-lf0-f49.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 37/23-07045-A95B4265 for ; Mon, 19 Oct 2015 05:19:23 -0400 Received: by lffv3 with SMTP id v3so105018262lff.0 for ; Mon, 19 Oct 2015 02:19:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=Rdmx5qw7z7rP5Lgs3Tk4xYb0w06nPoJuYpW5vJpCLCo=; b=op7hPk05y+i5UiZ/B8BPiVgDd377NRo7AnizSq4u8QiEhSt41El76cTV9LK4go+stp htkZjA3/mDQnHM3uAnXQCDR9SktfZssdWaOr+KiHDoO8YOSHwHBkltWUVTqreoeHnYAp Hm1GoaovbIW4l7gMXVCd0i8bCG4LUJBq4LGpGnJw4yCJqZhOivByAeObzrPK40tDNoZa +qwBkUWxcrOUGoLflrSod5A0/932FtPp2UI3HJxETLfqykIt7Z1DU2wP6PhRhuxGKQWQ BsEilX3PAHEZ5Zpk6YtgOospxyVLQl9os2961aqh20e8Q6j8LxwGyLROr7ZiC2zCYYsm 4WNQ== MIME-Version: 1.0 X-Received: by 10.180.223.102 with SMTP id qt6mr19175718wic.11.1445246358802; Mon, 19 Oct 2015 02:19:18 -0700 (PDT) Received: by 10.27.14.14 with HTTP; Mon, 19 Oct 2015 02:19:18 -0700 (PDT) In-Reply-To: References: <01f001d1052b$bed5cbb0$3c816310$@belski.net> <022301d1053b$06a91a50$13fb4ef0$@belski.net> <02c801d10588$9009fd80$b01df880$@belski.net> Date: Mon, 19 Oct 2015 11:19:18 +0200 Message-ID: To: Alexander Lisachenko Cc: Anatol Belski , Xinchen Hui , Dmitry Stogov , PHP internals Content-Type: multipart/alternative; boundary=001a1135f0b2d459e1052271a3d8 Subject: Re: [PHP-DEV] Re: Forbid rebinding scope of closures created by ReflectionFunctionAbstract::getClosure() From: nikita.ppv@gmail.com (Nikita Popov) --001a1135f0b2d459e1052271a3d8 Content-Type: text/plain; charset=UTF-8 On Mon, Oct 19, 2015 at 10:18 AM, Alexander Lisachenko < lisachenko.it@gmail.com> wrote: > Hello, internals! > > Just noticed this thread and want to clarify some things with getClosure() > method. If i understood correctly, ReflectionFunctionAbstract::getClosure() > should return non-rebindable closure ONLY for internal functions, but for > userland functions binding should be still possible, right? > > E.g. > > class Foo {private $a=1;} > > function test() { > var_dump($this); > } > $closure = (new \ReflectionFunction('test'))->getClosure(); > $object = new Foo; > $closure = $closure->bindTo($object, get_class($object)); > $closure(); > > (unfortunately, it's broken: https://3v4l.org/YeDBj#v700rc5) > > I know, that binding functions to objects is tricky, but this can be > useful for utility classes. But if you insist on this, ok, it's very rare > case, just one single example. > > What is more important for me is ability to work with methods as closures. > So my main concern is about restrictions of ReflectionMethod->getClosure() > and rebinding. > > See this example: > > class Foo {private $a=1;} > > class Bar { > function test() { > var_dump($this); > } > } > $closure = (new \ReflectionMethod('Bar','test'))->getClosure(new Bar); > $object = new Foo; > $closure = $closure->bindTo($object, get_class($object)); > $closure(); > > This kind of rebinding is heavily used by Go! Aspect-Oriented Framework > and now it will be broken (see https://3v4l.org/cZtbI#v700rc5). I think > there will be some more frameworks/libraries that use reflection and > closure rebinding via reflections. So this introduced patch for RC5 is > definitely a BC break for existing code. > Could you please provide a workaround or alternative ways to keep it > working? Maybe, restrict rebinding only for internal objects/methods will > be enough? Or only for ReflectionFunctionAbstract->getClosure(), but not > for ReflectionMethod->getClosure() > This change is primarily targeting userland methods, so your use-case is exactly the one this is supposed to prevent. Note that you can still use ->bindTo($object). The only thing you cannot do is ->bindTo($object, get_class($object)). Nikita --001a1135f0b2d459e1052271a3d8--