Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:84652 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 18729 invoked from network); 12 Mar 2015 19:34:59 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Mar 2015 19:34:59 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.181 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.181 mail-we0-f181.google.com Received: from [74.125.82.181] ([74.125.82.181:37347] helo=mail-we0-f181.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 54/25-24603-16AE1055 for ; Thu, 12 Mar 2015 14:34:57 -0500 Received: by wesx3 with SMTP id x3so18714682wes.4 for ; Thu, 12 Mar 2015 12:34:54 -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; bh=a2JO5Iw9G5eP7YFDZ5+Oss5rc1wWRh7JC8vZtNNtMJk=; b=hpyf8u/Cltv+LtSKOdYxviwwrQTKS+Jhitu0/b6VFmy639K+maGHZf4BHMyQTZi+8q XIT8r2v3JyOgF5CmRviv2bKAtRYvKKHKP33vU6Fgxe7u2HxCWKfTw3UMPOD4cPn+Fwco RXBZBrKFlni/ujntaUu6FAtJauVTHDdolqQFx4GiQYBlc08eCP9hmXCZTB8LIzqgPetc FZs+xEPNT0si0YlKJzrkAUZ/zjb3txSJk8c/qHJvs3CBWH4AgFT8XDxIFxnlxT8stzPk q0dfaahoie0N1nIVAa2I0/VUAW2hajKXjmKviMSSIxqsMj8nIoECmJfmg725YuGkdWaX snTA== X-Received: by 10.194.200.166 with SMTP id jt6mr45937586wjc.66.1426188894043; Thu, 12 Mar 2015 12:34:54 -0700 (PDT) Received: from [192.168.0.159] ([62.189.198.114]) by mx.google.com with ESMTPSA id bd1sm30748593wib.13.2015.03.12.12.34.52 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Mar 2015 12:34:53 -0700 (PDT) Message-ID: <5501EA37.4050905@gmail.com> Date: Thu, 12 Mar 2015 19:34:15 +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 References: <6D.2C.32765.10EC0055@pb1.pair.com> <5500D967.5040800@gmail.com> <13.69.64353.73451055@pb1.pair.com> <5501876C.3020107@gmail.com> <3D.85.42021.3E7A1055@pb1.pair.com> <5501B77D.5010800@gmail.com> <85.D1.24603.247C1055@pb1.pair.com> <5501D328.9040800@gmail.com> In-Reply-To: Content-Type: multipart/alternative; boundary="------------090506020402030900090403" Subject: Re: [PHP-DEV] static constructor From: rowan.collins@gmail.com (Rowan Collins) --------------090506020402030900090403 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Patrick Schaaf wrote on 12/03/2015 18:40: > > Am 12.03.2015 18:56 schrieb "Rowan Collins" >: > > > > Johannes Ott wrote on 12/03/2015 17:05: > > > >>>> So doing a null check each time > >>>> is a overhead of calculation which can be avoided with this static > >>>> constructor pattern. > >>> > >>> Presumably the engine would need to perform some implicit > equivalent of > >>> "if ( ! self::$initialised )" on each call to decide if the static > >>> constructor needs to be called or not, so the overhead is not > completely > >>> eliminated. > >>> > >> Yes you are right but I think it can be done more efficiently > inside the > >> interpreter using some struct flags then have to parse each time inside > >> a coded part in the application. > > > > Yes, point taken. > > I don't think such a flag is neccessary at all. Any class. at the > moment, comes from one or another file that is included / required. > And all of the code in these files outside the class definition, is > then immediately executed. The only thing neccessary would be to > check, just before that execution begins, which of the new classes > have such an initializer method, and then call that, before the > execution of the file itself begins. > This was my initial interpretation, but Johannes has explained that that is not the intention of this proposal. Instead, it is intended to be called on first *use* of the class; a subtle difference, but given this code: class A { public static $foo; private function __static() { echo 'A'; } } class B { public static $foo; private function __static() { echo 'B'; } } B::$foo = 1; A::$foo = 2; Running the magic at definition time will echo 'A' then 'B'; running it on first use will echo 'B' then 'A'. > Incidentally that is something that cannot be emulated with the > autoloader-does-it approach, because the autoloader can only do that > after the include/require is complete - i.e. code within the file will > not yet see the class as initialized (in that approach). For that scenario, the autoloader can immediately call $class_name::__static() or whatever. The only edge-case is when you try to also put loose code in your class definition files, but why would you need to? Regards, -- Rowan Collins [IMSoP] --------------090506020402030900090403--