Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97895 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 39119 invoked from network); 20 Jan 2017 18:00:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Jan 2017 18:00:39 -0000 Authentication-Results: pb1.pair.com smtp.mail=php@fleshgrinder.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=php@fleshgrinder.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain fleshgrinder.com from 77.244.243.89 cause and error) X-PHP-List-Original-Sender: php@fleshgrinder.com X-Host-Fingerprint: 77.244.243.89 mx108.easyname.com Received: from [77.244.243.89] ([77.244.243.89:39387] helo=mx108.easyname.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 18/F5-00729-54052885 for ; Fri, 20 Jan 2017 13:00:38 -0500 Received: from cable-81-173-135-7.netcologne.de ([81.173.135.7] helo=[192.168.178.20]) by mx.easyname.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1cUdUH-0000YX-8N; Fri, 20 Jan 2017 18:00:34 +0000 Reply-To: internals@lists.php.net References: To: Rasmus Schultz , PHP internals Message-ID: Date: Fri, 20 Jan 2017 19:00:29 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-DNSBL-PBLSPAMHAUS: YES Subject: Re: [PHP-DEV] Not autoloading functions From: php@fleshgrinder.com (Fleshgrinder) On 1/20/2017 8:04 AM, Rasmus Schultz wrote: > Just a quick thought. > > Since the autoloading functions proposal is stalled, how about allowing for > import of static functions instead? > > use function Foo::bar; > > bar(); // calls Foo::bar() > > There are two benefits to this approach: > > 1. There is immediate support for autoloading without any need for adoption > or support in existing autoloaders. > > 2. Pseudo-namespaces (abstract classes with stateless static functions) are > already widely practiced in PHP - a lot of existing code would be supported > as is. > > The syntax when calling functions would be the same. > > If we had function autoloading, we would likely collect related functions > in a file anyway - putting them in a class instead gives more less the same > exact result. > > The only drawback I can see, is the inability to import a whole set of > functions with one statement - but being explicit about external imports is > widely considered best practice for classes and interfaces, so why not for > functions. Yeah, it's a bit inconvenient, but at least we can move ahead > and leverage existing code without changes or BC breaks. It's not all bad. > It's better than nothing perhaps? :-) > > Thoughts? > I actually like this idea a lot, something similar is possible in Rust with Enums. ```rust pub use self::Foo::{A, B}; pub enum Foo { A, B } fn main() { let a = A; } ``` Loading a set of static methods could look similar. ```php final class Foo { public static function A(): self { return new self; } public static function B(): self { return new self; } } ``` ```php use function Foo::{A, B}; $a = A(); ``` -- Richard "Fleshgrinder" Fussenegger