Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:76444 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 75954 invoked from network); 11 Aug 2014 22:15:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Aug 2014 22:15:03 -0000 Received: from [127.0.0.1] ([127.0.0.1:21071]) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ECSTREAM id 84/61-03357-76049E35 for ; Mon, 11 Aug 2014 18:15:03 -0400 Authentication-Results: pb1.pair.com smtp.mail=jwatzman@fb.com; spf=unknown; sender-id=unknown Authentication-Results: pb1.pair.com header.from=jwatzman@fb.com; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain fb.com does not designate 67.231.153.30 as permitted sender) X-PHP-List-Original-Sender: jwatzman@fb.com X-Host-Fingerprint: 67.231.153.30 mx0b-00082601.pphosted.com Linux 2.5 (sometimes 2.4) (4) Received: from [67.231.153.30] ([67.231.153.30:36767] helo=mx0b-00082601.pphosted.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C5/10-03357-B4B39E35 for ; Mon, 11 Aug 2014 17:53:15 -0400 Received: from pps.filterd (m0004077 [127.0.0.1]) by mx0b-00082601.pphosted.com (8.14.5/8.14.5) with SMTP id s7BLqTwm030016 for ; Mon, 11 Aug 2014 14:53:54 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=fb.com; h=from : to : cc : subject : date : message-id : references : in-reply-to : content-type : content-id : content-transfer-encoding : mime-version; s=facebook; bh=v2HRfEeqoeHLWtibnJAX6bALyP1FJ6DDuJtPc8Ct1Go=; b=l5L9fFMe/r7AAaXoOlggqACEWx0eIppxkOuhCY6Ry5LouvDsOYY5KA3moROgTtBFBE8e 6EkO7GvsavlR/UBOzpL/97ymKj7EhEuaos/2AK8fGV4FoT5IM9IH84z0dNsI6+81FyBa RLmxqZP/u5oE9NwQfKcepKU6Z1TBxo+0OkY= Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-00082601.pphosted.com with ESMTP id 1nq5n6831a-1 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Mon, 11 Aug 2014 14:53:53 -0700 Received: from m0004077 (m0004077 [127.0.0.1]) by pps.reinject (8.14.5/8.14.5) with SMTP id s7BLrrG6031042 for ; Mon, 11 Aug 2014 14:53:53 -0700 Received: from mail.thefacebook.com (mailwest.thefacebook.com [173.252.71.148]) by mx0b-00082601.pphosted.com with ESMTP id 1nq5n68315-2 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=OK); Mon, 11 Aug 2014 14:53:53 -0700 Received: from PRN-MBX02-2.TheFacebook.com ([169.254.5.69]) by PRN-CHUB11.TheFacebook.com ([fe80::80d:37ff:4b6a:a4fc%12]) with mapi id 14.03.0195.001; Mon, 11 Aug 2014 14:53:47 -0700 To: Andrea Faulds CC: Rowan Collins , "internals@lists.php.net" Thread-Topic: [PHP-DEV] [RFC] Closure::call and Function Referencing as Closures Thread-Index: AQHPr2UX6cWu/Jgb6EeTLZp+MQ2x55vAdcmAgABbUYCAAH23AIAAAi0AgAUGAgCABE79gIABzgQA Date: Mon, 11 Aug 2014 21:53:46 +0000 Message-ID: <301B540D-FF37-4293-A2BD-2A345D84BDB8@fb.com> References: <53DF2BED.10103@sugarcrm.com> <06F3EF08-21B1-49AD-9D9D-5043C69AC1D8@ajf.me> <53DFE1FC.5040206@gmail.com> In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [192.168.16.4] Content-Type: text/plain; charset="Windows-1252" Content-ID: <3AB52E64790E924A9631A6A37BBAC827@fb.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.12.52,1.0.27,0.0.0000 definitions=2014-08-11_04:2014-08-11,2014-08-11,1970-01-01 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 kscore.is_bulkscore=3.57199997358393e-09 kscore.compositescore=0 circleOfTrustscore=71.128 compositescore=0.999749537164123 urlsuspect_oldscore=0.999749537164123 suspectscore=0 recipient_domain_to_sender_totalscore=0 phishscore=0 bulkscore=0 kscore.is_spamscore=0 recipient_to_sender_totalscore=0 recipient_domain_to_sender_domain_totalscore=8891 rbsscore=0.999749537164123 spamscore=0 recipient_to_sender_domain_totalscore=0 urlsuspectscore=0.9 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=7.0.1-1402240000 definitions=main-1408110250 X-FB-Internal: deliver Subject: Re: [PHP-DEV] [RFC] Closure::call and Function Referencing as Closures From: jwatzman@fb.com (Josh Watzman) On Aug 10, 2014, at 11:20 AM, Andrea Faulds wrote: > Hi! >=20 > Sorry for the slow response, I=92ve been on holiday. No problem! > On 8 Aug 2014, at 01:32, Josh Watzman wrote: >=20 >> The RFC goes a long way to fixing this, but one important place it misse= s is with function references to private and protected methods. The crux of= the issue is that allowing an unbound closure to escape can lead to very u= nexpected and unwanted results, as opposed to forcing the closure to be bou= nd to a particular variable. Consider: >>=20 >> > class C { >> private function priv() { /* ... */ } >> public function pub() { >> // ... do stuff ... >> // Return a closure for the caller to call priv at a later date: >> return &self::priv; >> } >> } >>=20 >> While this example is somewhat contrived, I've certainly wanted to do so= mething similar before. The intent of this code is for the closure returned= by "pub" to only ever be called on what was $this inside "pub" -- but not = only can you change that with bind() on the closure, "pub" itself cannot ev= en pre-bind it to the intended $this. Or maybe it technically could by call= ing bind() on the result of &self::priv before returning it, but that's ver= y cumbersome for what is the common case for what you want to do with "func= tion pointer" to a non-static method. >=20 > Unfortunately, yes, such closures are not pre-bound. However invoking it = when it=92s unbound will produce the same error as calling any method stati= cally (E_STRICT or E_ERROR), so the issue can be noticed and fixed easily. = It=92s possible to bind with Closure::bind(&Foo::bar, =91Foo=92); or someth= ing like that. I could make it pre-bind, but I=92m unwilling to as :: never= binds when calling, so it shouldn=92t when referencing for consistency. My point is not that you might forget to bind() the Closure, agree that it'= s taken care of just fine as-is. My point is that there's no way for pub() = to express that it only ever expects the closure returned to be bound to th= e object on which you called pub(). Not only is the caller free to bind it = to some other variable, but the bug might not be obvious at all since there= 's nothing constraining the call to bind() to happen nearby to the code in = pub() where the intent that it only be bound to that object is clear. Every= thing here is conventional, when IMO it should be contractual, most especia= lly since an unbound Closure isn't even meaningful to use (except to bind i= t before using it). >> You can't do any of those things to the "real" function, so it's unclear= if it makes sense to be able to do it to the callable reference. >=20 > Well, closures are immutable, to be fair. bind and bindTo actually produc= e *new* functions, and they don=92t even work on static closures anyway. Yo= u=92re not really doing these things to the =93real=94 function. Ah, whoops -- it's apparently an HHVM-ism that you can add properties to Cl= osures :) http://3v4l.org/adEgO The rest of the point I was making here bas= ically boils down to being able to bind/re-bind them, which is the same as = the one I'm trying to make above, so I won't belabor it. Josh Watzman