Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98003 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 50539 invoked from network); 28 Jan 2017 14:13:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Jan 2017 14:13:15 -0000 Authentication-Results: pb1.pair.com header.from=rasmus@mindplay.dk; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=rasmus@mindplay.dk; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mindplay.dk from 209.85.220.182 cause and error) X-PHP-List-Original-Sender: rasmus@mindplay.dk X-Host-Fingerprint: 209.85.220.182 mail-qk0-f182.google.com Received: from [209.85.220.182] ([209.85.220.182:35596] helo=mail-qk0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B4/70-28703-9F6AC885 for ; Sat, 28 Jan 2017 09:13:13 -0500 Received: by mail-qk0-f182.google.com with SMTP id u25so89566987qki.2 for ; Sat, 28 Jan 2017 06:13:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mindplay-dk.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=aRhgdz1vHiqTQXs8bwjuS5D9PCc59QYc0BK9IqPV5b8=; b=goQhYUCi81tfnyhW1G7hAfiTEYE3ymthViDoiq6KwzJWG0ICXo7y+2pTEEMikFIESS 1DpwYzziFNiWUDs01JUsssbUoFfsch/5RliJzIQv5UH0k32he9J95Nn467P/ohErq3a9 YD+iHj5uiH2NitMbqC8fJHLT8P8WA3ESXscl6yPRFhOia1cVkqW78Uagg5/1qYMm0TaP 8sFGi4cPlpWzMRa1wCIVxvsNjURTVn4E+L0EQmrVDjcKrQ7EU+AfKqOHRk0OoE8RH86s NSs3mRZSerbs+ymoZGAsFk807isqhEclAh1vTK/KqZ2adpJ3CTiKNhWz3t3l5y5k2bak Qg6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=aRhgdz1vHiqTQXs8bwjuS5D9PCc59QYc0BK9IqPV5b8=; b=safcx+tHdpwyBWIytWCLBBiXHjtMW4cNbI8nu863B8mkB5n/GWNGo16fRNqZZO6nZ7 azj8pMCxST/p88GIcjvPVcFeLYnTP3LW27h34s3Nusbj/48hu8sOgMMNE8Mryi66ZbH8 5vg/VuI85w7Jy2+dSge73yzRxZHI17o9CGQfRb5slSuxXLweNc8Z0em5GcU3Elti+WQE rdiCq4Faw4DzUxUAnvhMS6mkk6eEHu7iXb4IpNvrkxxcQkzM64gU1BwhnqxwxT36Z+bL WOD5E7NAWofggktXW4K8anf1VEusYdSqUWDftCQtBckr5kUVilLT8wu+39k2OJ5/yxeM U39g== X-Gm-Message-State: AIkVDXKqf8Lg96HcS/Xv0+xmUsxtFF6Zh6LqLIuj47gU2pyWa/yeQXoUrYeSKlN9qT9Zpxj2i1u391VGnBx/9w== X-Received: by 10.55.138.196 with SMTP id m187mr13294470qkd.214.1485612790912; Sat, 28 Jan 2017 06:13:10 -0800 (PST) MIME-Version: 1.0 Received: by 10.237.47.231 with HTTP; Sat, 28 Jan 2017 06:13:10 -0800 (PST) In-Reply-To: References: <9113D065-B94A-4CA2-9420-56963C703ECA@gmail.com> Date: Sat, 28 Jan 2017 15:13:10 +0100 Message-ID: To: Wes Cc: Rowan Collins , PHP Internals Content-Type: multipart/alternative; boundary=94eb2c073e0aad30d90547282e32 Subject: Re: [PHP-DEV] Not autoloading functions From: rasmus@mindplay.dk (Rasmus Schultz) --94eb2c073e0aad30d90547282e32 Content-Type: text/plain; charset=UTF-8 > old applications could maintain compatibility only > by adding a simple autoloader that would alias the global function/constant > into the calling namespace So as a side-effect of calling any function, my namespace would get polluted with all sorts of random names? That sounds like a messy work-around. Okay, so take it back for a moment. The problem case is this, right? namespace Foo; bar(); If the function Foo\bar() has not already been loaded, we fall back to bar() in the global namespace. With a function-autoloader in place, we would need to trigger this for every function-call from any namespace, and that's unacceptable. Okay, well, existing code does not expect function autoloading and wouldn't benefit from it without changes, right? So what if, instead of trying to autoload everything by default, we make it opt-in? Something like: namespace Foo; use namespace Foo; bar(); Now, if function Foo\bar() has not been defined yet, and function \bar() doesn't exist, then start trying trying to autoload them from used namespaces. Okay, that may seem a bit redundant - having to explicitly indicate I'd like to be able to call functions from within the file's own namespace. But it'll make more sense if you put your functions in a functions-only namespace: namespace Foo; use namespace Foo\Functions; bar(); This call would prefer Foo\Functions\bar() if it is able to autoload that, otherwise will try \bar(). Or if you have several function-namespaces: namespace Foo; use namespace Click, Clack; bar(); This would prefer Click\bar(), then Clack\bar(), if any of those can autoload, otherwise will try \bar(). Now, this is still a lot of autoload queries, e.g. one for every function called in every file. It also doesn't address the issue of autoloading constants. So. What if we don't autoload functions at all? What if we autoload namespaces instead? That reduces the number of autoload queries from one per function to one per namespace, which might be more acceptable?? It does mean that individual functions and constants cannot be autoloaded - the smallest unit that can autoload will be a namespace. I think that's an okay limitation? For the most part, packages will likely want to ship a set of functions (and/or constants) as a single file anyway. If a vendor has too many functions and don't want the overhead of loading them as a single file, they likely can/should be organized into several (sub) namespaces anyhow, so maybe that's acceptable. So by default, there would be no namespace-autoloading for the file's own namespace - we don't query the namespace-autoloader by default. As for use function: namespace Foo; use function Blip\bar(); bar(); This would trigger namespace auto-loading for Blip when bar() is invoked. So existing code with function imports could switch to namespace-autoloading. Code that currently uses include/require to load functions into a file-namespace will need to remove their include/require statements, so needs changes anyhow to use namespace-autoloading - if this code also needs an added "use namespace" statement to achieve namespace-autoloading, that's probably okay? So yeah, this form of autoloading is a bit inconsistent with the name resolution rules - but it doesn't require any change as drastic as changing the function name resolution rules, it's pretty simple, and (I think?) backwards-compatible. It could also potentially save you a lot of explicit "use function" statements, since you can replace ranges of function-imports (from the same namespace) with a single namespace-import. When new functions are added to imported namespaces, they would be immediately callable, without needing to add more imports first. One drawback or risk of this approach, is of files importing multiple namespaces, e.g.: use Foo, Bar; baz(); If you're currently calling Bar\baz() and then a function Foo\baz() is introduced, unwittingly you would now be calling that. That's a pretty marginal issue though - function name collisions probably aren't very likely to happen, especially within the same file. Thoughts? On Fri, Jan 27, 2017 at 4:29 PM, Wes wrote: > Importing functions from a static class would equally require to have all > functions in the same file. I was proposing an alternative to that, not > "symbol to file" autoloading. > > Though, I know that problem well, sadly. I've suggested PHP should just > deprecate the fallback to root namespace, and remove the feature altogether > in PHP8. > > Imho it's the only way to actually solve this. Some say it would be a huge > BC break, but in reality old applications could maintain compatibility only > by adding a simple autoloader that would alias the global function/constant > into the calling namespace. Roave wrote this > https://github.com/Roave/FunctionFQNReplacer , PHPStorm is supporting the > no-fallback referencing: > https://youtrack.jetbrains.com/issue/WI-27425 (recently implemented) > https://youtrack.jetbrains.com/issue/WI-34446 (recently implemented) > https://youtrack.jetbrains.com/issue/WI-34858 > So... it could be the right time to deprecate the fallback to root > namespace, before PHP8 gets too close. > --94eb2c073e0aad30d90547282e32--