Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:94950 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 24646 invoked from network); 8 Aug 2016 20:23:31 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Aug 2016 20:23:31 -0000 Authentication-Results: pb1.pair.com header.from=me@daveyshafik.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=me@daveyshafik.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain daveyshafik.com from 209.85.216.178 cause and error) X-PHP-List-Original-Sender: me@daveyshafik.com X-Host-Fingerprint: 209.85.216.178 mail-qt0-f178.google.com Received: from [209.85.216.178] ([209.85.216.178:36517] helo=mail-qt0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 81/B8-33134-14AE8A75 for ; Mon, 08 Aug 2016 16:23:30 -0400 Received: by mail-qt0-f178.google.com with SMTP id 52so209433267qtq.3 for ; Mon, 08 Aug 2016 13:23:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daveyshafik-com.20150623.gappssmtp.com; s=20150623; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=MxzzTrr16YpjKz7yAzz8fgvFILTOoZLSjt/Fqj3LlgA=; b=Dy2xxVpy/38al498U3OorjzVJPIUWd9ZjodF9ndItIAiXr2Wvu+FcFJSM5Bai6Fv1n KEC6gDZ5CBEtqxMF4fQw4LS5PLwAptZjx0VJpXX1QfPoA4yChNzn7vTxRYx2qqnTbLF5 g6zWV3QuwRw5hYLY/jdoZtAV1U97tMBABkjXljmd5J6cBRMPVg4fHKO/z0AjeWvh3d/b tELNHO8Y1F3p9mCAMIUW6nH/1uof7NU24wlvcXbOJb5n79bpqlzkVHI33VgO0dO6P7wP LfbiEYmIMk7GBR6dOZ3bRYJaTm7N1F+M4iYmiRM/BqctPHSGnQcy2K3mrDt7IXnIUhxU k/MQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=MxzzTrr16YpjKz7yAzz8fgvFILTOoZLSjt/Fqj3LlgA=; b=ENVwVEjBnfHMq1jdRZKyUT5QYYIK7ruwAOJKKCE+FY9J7ZKchsYjT2c/Q0MGSyM20G dHL8JRNUzKjuNRAO2/bKwTdFWzMM7mRLLhccHN7nqBWUM/3WDFfLXWLaFEPeJ1+uqnuP F/VpMsIQaPw0fNssVcuA+uB0ye7RGbu8mibsG3SOZaOxI7Fs0WRGjTnBMogkLADY4/cJ oW1rRsGwxK3vB1+AkSmhr6vcURQfefZCYOkieb7Fj3wvJEHm3jltTzMcpjrFwjCDP6F5 lQ7wwZA/yX2kuj4QZciqiSXJQrx4sxSiGhK2lu5JniekczEoT82VpDrFogsZAYd63Htp 92Ag== X-Gm-Message-State: AEkoouuWkt6xXkuuIzibjFm91aD9BOBfoyVoJFJ/+D4jod9UB/yt4N8dUHRRjhxdUIY7yORGgLmCU6QbfLqqLFQO X-Received: by 10.200.45.8 with SMTP id n8mr31052156qta.57.1470687806964; Mon, 08 Aug 2016 13:23:26 -0700 (PDT) MIME-Version: 1.0 Sender: me@daveyshafik.com Received: by 10.237.55.138 with HTTP; Mon, 8 Aug 2016 13:23:26 -0700 (PDT) In-Reply-To: References: <33781781-2a63-78c1-68a1-9e19ad720d8d@gmail.com> <1d89ec68-de5e-2670-aed1-f12872c073c3@gmail.com> Date: Mon, 8 Aug 2016 13:23:26 -0700 X-Google-Sender-Auth: C9xN4H7EMzCLloj4Eovgf-GT0EU Message-ID: To: Rasmus Schultz Cc: Rowan Collins , internals Content-Type: multipart/alternative; boundary=001a1149dab24f6026053995307a Subject: Re: [PHP-DEV] Function auto-loading From: davey@php.net (Davey Shafik) --001a1149dab24f6026053995307a Content-Type: text/plain; charset=UTF-8 On Mon, Aug 8, 2016 at 1:15 PM, Rasmus Schultz wrote: > > Unfortunately, function name resolution has this quirk that class name > resolution doesn't, so something's got to give. > > I suppose. > > Well, then, how about making the feature work consistently for all > functions, by coupling it directly to the "use function" statement? > > In other words, the feature changes from being strictly about auto-loading > functions, to instead resolving an imported function - so it would be > triggered by the "use function" statement only, rather than by the use of > the function, and the resolved function pertains only to the scope of the > file. > > A function resolver would simply need to return a callable: > > register_function_resolver(function ($name) { > if ($name === "html") { > return function ($str) { > return htmlspecialchars($str, ENT_HTML5); > }; > } > > if (substr($name, 0, 5) === "iter\\") { > require_once VENDOR_PATH."/nikic/iter/src/bootstrap.php"; > return $name; > } > }); > > This mechanism is probably a lot easier to explain and understand - and > works equally for global or namespaced functions. > > It also lets you potentially do other weird shit, like overriding > var_dump() or hooking into certain function calls - which could enable you > to do a bunch of evil, but I'm not really big on complicating features > solely to keep users from doing harm; simpler language features tend to > allow you to do more stuff - good or evil. > > Likely the 99% use-case is simply for Composer to bootstrap your packages > for you, and this feature will permit you to do that. > > Okay, so it doesn't deal with namespaced constants, and maybe this is me > being opinionated, but who's going to import constants one by one? > Constants are usually better off grouped together in a class. Although I > don't suppose there's any reason this concept couldn't be expanded to work > for constants as well, for completeness at least - though I have doubts > that very many people would care... > > Anyways, just spitballing here :-) > > > On Mon, Aug 8, 2016 at 7:54 PM, Rowan Collins > wrote: > > > On 08/08/2016 18:03, Levi Morrison wrote: > > > >> If not, I don't see why we ever need to be able to autoload global > >> functions. "You want autoloading? Put it in a namespace." Like I > >> say, that leaves the very small edge case of a single namespace > >> spanning multiple files, and an autoloader implementation able to > >> include one of them when a function is called from another. > >> > >> > >> I'm not sure why you would think a single namespace spanning multiple > >> files is a "very small edge case". I disagree. Here are some libraries I > >> am aware of *off the top of my head* that use functions the same > >> namespace across multiple files: > >> > >> * https://github.com/nikic/iter > >> * https://github.com/lstrojny/functional-php > >> > >> As well as several of my personal projects. I do not think this is a > >> "very small edge case." > >> > > > > The "iter" example looks a long way from being autoloadable whatever we > > supported, but the example of one-function-per-file is definitely > relevant, > > so I stand corrected. > > > > After a bit of clicking, I even managed to find a line which would fail > to > > autoload under the proposed limitation: > > > > https://github.com/lstrojny/functional-php/blob/master/src/ > > Functional/CompareObjectHashOn.php > > > > > return compare_on($comparison, $keyFunction); > > > > Although interestingly, at the top of the file there is a (technically > > unnecessary) "use function Functional\compose;" If there was a "use > > function Functional\compare_on;" as well, we'd be fine. (The function > name > > would then become qualified at compile time and trigger autoloading at > run > > time.) > > > > > > On 08/08/2016 18:06, Rasmus Schultz wrote: > > > Unless there's a demonstrated, critical performance issue with > > > auto-loading of global functions, please, let's not cripple this > feature > > > with inconsistencies from the get-go! > > > > Sure, we could try to measure it, but remember that it's not just the > > engine code that has the extra cost, it will actually call a userland > > function every time you use a global function from inside a namespace if > > you don't add a leading "\". That userland function will probably do a > > bunch of string comparisons before deciding it's not interested, and may > > even try to stat a file or two. Those are really expensive operations, > so I > > think it's a long way from "micro-optimisation". > > > > Unfortunately, function name resolution has this quirk that class name > > resolution doesn't, so something's got to give. > > > > > > Regards, > Related to this, I'd like to see stream (and stream filter) autoloading. Essentially, if you fopen("random://foo") it'll pass that string to an autoloader that will load and register the stream. It fails if at the end of the autoloader the stream isn't registered. Similar for filter and stream_filter_(pre|ap)pend() and loading stream filters. - Davey --001a1149dab24f6026053995307a--