Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:84634 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 61345 invoked from network); 12 Mar 2015 12:33:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Mar 2015 12:33:25 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.171 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.171 mail-wi0-f171.google.com Received: from [209.85.212.171] ([209.85.212.171:36120] helo=mail-wi0-f171.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D4/23-42021-59781055 for ; Thu, 12 Mar 2015 07:33:25 -0500 Received: by wiwh11 with SMTP id h11so36899744wiw.1 for ; Thu, 12 Mar 2015 05:33:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=v+5EBFHfxWB0eoEYVgLkfDNHAFs6yG5VIEAtpyalwWg=; b=JyhmkLKcMts05j0vlv03w9PZrdVKXS20+5N5GXYUkXI4E6psw72r1b3QoeH8NoCO4A VRehghsYrITv+ZchcZMU/WHk473iFJqMadA0mrmKn1XyZjsNTEfTFPde9fAmlh+ZWQJL TKqCtSPPdmZVYl0mQdzl6itMp9m0Jk4OQq7ib8hB3HIYVtWOIbTGRxUXJRZ7vj3LVfFX AJVKJv1FD9RNlY1josvJFuIWXUPNLkk0BJweIVsRqqV/Qp4tBVIpm9YyyN6JuLkTDSnU zs8i7Rg9WDPpaXkVlBFh9Er676DjzAIrY6Z67A5x8AHvHy7sBwp6YmmM1SJiGmrqAw5X 7CkA== X-Received: by 10.180.79.1 with SMTP id f1mr18356188wix.24.1426163601312; Thu, 12 Mar 2015 05:33:21 -0700 (PDT) Received: from [192.168.0.159] ([62.189.198.114]) by mx.google.com with ESMTPSA id bd3sm10504782wib.17.2015.03.12.05.33.19 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Mar 2015 05:33:20 -0700 (PDT) Message-ID: <5501876C.3020107@gmail.com> Date: Thu, 12 Mar 2015 12:32:44 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: internals@lists.php.net References: <6D.2C.32765.10EC0055@pb1.pair.com> <5500D967.5040800@gmail.com> <13.69.64353.73451055@pb1.pair.com> In-Reply-To: <13.69.64353.73451055@pb1.pair.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] static constructor From: rowan.collins@gmail.com (Rowan Collins) Johannes Ott wrote on 12/03/2015 08:54: > Am 12.03.2015 um 05:17 schrieb Levi Morrison: >> On Wed, Mar 11, 2015 at 6:10 PM, Rowan Collins wrote: >>> On 11/03/2015 23:21, Johannes Ott wrote: >>>> So now I want to do my first own proposal for a new function in PHP and >>>> I hope doing it right with starting a discussion here first. >>>> >>>> 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... >> Notably, user-land enums. You don't want public constructors because >> you don't want it instantiated except for each enum property. You also >> need it to run on class creation, not afterwards. >> >> I think we'd be better off adding language-level enums than static >> constructors though. >> > Yes indeed user-land enums are one of the use cases I use this feature > at the moment. But there some other use cases as well: Most of these examples are just crying out to be real objects, not static classes. You might not want to be creating them every time you use them, but that's what patterns like Singletons and Dependency Injection are for. > 1. Nearly all of my classes have a static LogAdapter $LOG which has to > be intialized with Classname once. > > class A { > private static $LOG; > > public static function __static() { > self::$LOG = LogAdapter::getLogger(self::class); > } > } A::__static(); > > The LogAdapter by it selfs although have a __static() method to prepare > the Log4PHP-Framework I'm using. This particular example could be achieved with a static getLogger() method, which does the initialisation check the first time the logger is needed, rather than the first time class A is needed. To my mind, the creation of a class in memory should not have side-effects - you should be able to assume that all your classes exist at the same time, before any of your code runs. The only justification for not acting that way would be Python-style metaclasses, where the creation of a class definition was the responsibility of some other object, and that's a power to be wielded very carefully. Regards, -- Rowan Collins [IMSoP]