Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:79339 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 15886 invoked from network); 1 Dec 2014 14:32:51 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Dec 2014 14:32:51 -0000 Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 192.64.116.200 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 192.64.116.200 imap1-2.ox.privateemail.com Received: from [192.64.116.200] ([192.64.116.200:42840] helo=imap1-2.ox.privateemail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 55/90-12174-E0C7C745 for ; Mon, 01 Dec 2014 09:32:48 -0500 Received: from localhost (localhost [127.0.0.1]) by mail.privateemail.com (Postfix) with ESMTP id 4B7EBB0009C; Mon, 1 Dec 2014 09:32:44 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at imap1.ox.privateemail.com Received: from mail.privateemail.com ([127.0.0.1]) by localhost (imap1.ox.privateemail.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id GucPFsIUybnb; Mon, 1 Dec 2014 09:32:44 -0500 (EST) Received: from oa-res-27-210.wireless.abdn.ac.uk (oa-res-27-210.wireless.abdn.ac.uk [137.50.27.210]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.privateemail.com (Postfix) with ESMTPSA id 66252B00098; Mon, 1 Dec 2014 09:32:42 -0500 (EST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.1 \(1993\)) In-Reply-To: <003c01d00d6e$f4fd77c0$def86740$@tutteli.ch> Date: Mon, 1 Dec 2014 14:32:41 +0000 Cc: internals , guilhermeblanco@gmail.com Content-Transfer-Encoding: quoted-printable Message-ID: <868FD3D8-4E80-46F4-872A-125D3FD8F40D@ajf.me> References: <003c01d00d6e$f4fd77c0$def86740$@tutteli.ch> To: Robert Stoll X-Mailer: Apple Mail (2.1993) Subject: Re: [PHP-DEV] [RFC] Static classes (Was Abstract final classes) From: ajf@ajf.me (Andrea Faulds) Hi all! > On 1 Dec 2014, at 13:58, Robert Stoll wrote: >=20 > I read your updated RFC: > https://wiki.php.net/rfc/abstract_final_class Hmm, I don=E2=80=99t like this new RFC either. Static classes in = languages like C# exist because the language designers made the dogmatic = decision to require everything to be a class. Thus, to make collections = of functions, your only choice is to make a =E2=80=9Cutility class" with = a bunch of static methods. To make this easier, the `static` keyword was = added to make this easier in C# - Java doesn=E2=80=99t have this = keyword, but it has the same pattern of static method-only classes. But = PHP is not one of these dogmatic =E2=80=9Ceverything must be a class=E2=80= =9D languages: it has true global functions which can be namespaced. So, why, then, does PHP need static classes? As I see it, there are two = reasons: (1) Functions currently cannot be autoloaded =20 (2) Encapsulation of state (private/protected static properties) I think both of these can be solved without perpetuating what I feel is = an anti-pattern, the =E2=80=9Cutility class=E2=80=9D. If they are = solved, the need for utility classes goes away. To implement (1), someone just needs to go and finally implement = function autoloading. I think Joe Watkins (krakjoe) might=E2=80=99ve = said he=E2=80=99d write a patch for that, but I may be wrong. I=E2=80=99d = certainly like to help with this effort. Implementing (2) is more difficult. We currently don=E2=80=99t have = file-local variables like C has, we don=E2=80=99t even have namespaced = variables. We do have static variables within functions, however. I can = think of a few possible approaches to this: (1) Say that global state is evil and refuse to implement it. Some = people (myself included, to an extent) would argue that global state is = =E2=80=9Cspooky action at a distance=E2=80=9D and we shouldn=E2=80=99t = be encouraging it. In this case we encourage users to simply make = normal, non-static/abstract classes and to pass an instance around. This = is generally good programming practise anyway. (2) Add lexically-scoped variables, and allow normal global functions = to be closures. By this I mean we add something like JavaScript=E2=80=99s = `let` keyword that would make a variable that is unset when it falls out = of scope (end of a {} block, end of a file etc.). Then, we allow normal = functions to use the `use ($var)` syntax and close over variables. = That=E2=80=99d look something like this: