Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:91503 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 38763 invoked from network); 4 Mar 2016 18:29:51 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Mar 2016 18:29:51 -0000 X-Host-Fingerprint: 68.118.157.39 68-118-157-39.dhcp.mdsn.wi.charter.com Received: from [68.118.157.39] ([68.118.157.39:10220] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 3C/51-25279-E14D9D65 for ; Fri, 04 Mar 2016 13:29:50 -0500 Message-ID: <3C.51.25279.E14D9D65@pb1.pair.com> To: internals@lists.php.net References: <24.F3.62769.A4226C65@pb1.pair.com> <1457099585.14527.13.camel@kuechenschabe> Date: Fri, 4 Mar 2016 12:29:45 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 MIME-Version: 1.0 In-Reply-To: <1457099585.14527.13.camel@kuechenschabe> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Posted-By: 68.118.157.39 Subject: Re: [PHP-DEV] Re: [RFC] Deprecations for PHP 7.1 From: me@stephencoakley.com (Stephen Coakley) On 03/04/2016 07:53 AM, Johannes Schlüter wrote: > On Thu, 2016-02-18 at 19:57 +0000, Andrea Faulds wrote: >> I've actually used create_function on occasion for programmatically >> generating functions (in particular to create a function for each PHP >> operator), but it's trivially polyfillable, and it'd be better if >> people >> were implementing it themselves (and thus knowing it's based on >> eval()) >> than using it directly without this knowledge. > > Just as a detail: You can't create a 100% compatible polyfill to > create_function(). create_function() names functions starting with \0. > Functions starting with \0 are hidden from get_defined_functions() and > possibly other places. > > php > echo sizeof(get_defined_functions()['user']); > 0 > php > create_function('', ''); > php > echo sizeof(get_defined_functions()['user']); > 0 > php > eval('function foo() {}'); > php > echo sizeof(get_defined_functions()['user']); > 1 > > johannes > Good point. Another approach to a polyfill would be to use a closure then: function create_function(string $args, string $code) { return eval("return function($args) { $code };"); } $newfunc = create_function('$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);'); echo $newfunc(2, M_E) . "\n"; Not a complete polyfill, since $newfunc is a closure and not a string, so you cannot print out the function name. I have no idea if any code relies on the lambda itself being a string though. -- Stephen