Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:84615 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 19091 invoked from network); 12 Mar 2015 08:40:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Mar 2015 08:40:41 -0000 Authentication-Results: pb1.pair.com smtp.mail=php@bof.de; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=php@bof.de; sender-id=pass Received-SPF: pass (pb1.pair.com: domain bof.de designates 80.242.145.70 as permitted sender) X-PHP-List-Original-Sender: php@bof.de X-Host-Fingerprint: 80.242.145.70 mars.intermailgate.com Received: from [80.242.145.70] ([80.242.145.70:55756] helo=mars.intermailgate.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FB/F8-64353-80151055 for ; Thu, 12 Mar 2015 03:40:41 -0500 Received: (qmail 12117 invoked by uid 1009); 12 Mar 2015 09:40:37 +0100 Received: from 192.109.53.146 by mars (envelope-from , uid 89) with qmail-scanner-1.25-st-qms (clamdscan: 0.96.2/20182. spamassassin: 3.3.1. perlscan: 1.25-st-qms. Clear:RC:0(192.109.53.146):SA:0(1.3/8.0):. Processed in 0.564355 secs); 12 Mar 2015 08:40:37 -0000 X-Spam-Status: No, hits=1.3 required=8.0 X-Spam-Level: + X-Antivirus-MYDOMAIN-Mail-From: php@bof.de via mars X-Antivirus-MYDOMAIN: 1.25-st-qms (Clear:RC:0(192.109.53.146):SA:0(1.3/8.0):. Processed in 0.564355 secs Process 12105) Received: from unknown (HELO rofl.localnet) (gmail@bof.de@192.109.53.146) by mars.intermailgate.com with AES256-SHA encrypted SMTP; 12 Mar 2015 09:40:36 +0100 To: internals@lists.php.net Cc: Rowan Collins Date: Thu, 12 Mar 2015 09:40:35 +0100 Message-ID: <1806447.6ZrGY6hLx0@rofl> User-Agent: KMail/4.14.4 (Linux/3.19.0-5.g748552c-desktop; KDE/4.14.4; x86_64; ; ) In-Reply-To: <5500D967.5040800@gmail.com> References: <6D.2C.32765.10EC0055@pb1.pair.com> <5500D967.5040800@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Subject: Re: [PHP-DEV] static constructor From: php@bof.de (Patrick Schaaf) On Thursday 12 March 2015 00:10:15 Rowan Collins wrote: > On 11/03/2015 23:21, Johannes Ott wrote: > > > The purpose of this suggestion is to introduce a static constructor, > > which is called before the first call to class either static or > > non-static to initialize some static properties which are needed by the > > class. > > Can you give an example use case for when this would be useful? I'm > struggling to think of one for which there isn't already an established > coding pattern... It's useful everywhere you now have more than one method starting if (!static::$is_initialized) static::initialize_me(); Some examples from our codebase: - a session wrapper class, hiding $_SESSION behind setter/getter methods, where the static class initialization determines which cookie to use, depending on some global feature flags, and which session backend to use, depending on current availability. (main purpose, apart from clean calling side code just using the setters/getters, is to get the lazy_write functionality Yasuo tried to introduce recently) - computation of some class properties from others, like doing an array_flip on one to get a reverse mapping. - definition of computed constants, in various places. Partly obsolete now that class constants support constant expressions, but needed as soon as these are not really constant. - setting up some class properties used by various methods, that should depend on some global feature flags (defined by the users of the class, usually toplevel scripts) - invariant checks on subclasses, in various places, where concrete subclasses set up some static properties of a configuration nature, and I want to make sure in the base class, as early as possible, that the values are consistent and make sense, avoiding checks spread all over the place in various methods. Of course, most of this could be done by code outside the class definition, in the same file. But that has proven, in the past, a fountain of joy wrt. placement, with variations needed for APC and opcache, and general frustration all around. best regards Patrick