Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:84681 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 25813 invoked from network); 13 Mar 2015 13:37:10 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Mar 2015 13:37:10 -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 74.125.82.180 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.180 mail-we0-f180.google.com Received: from [74.125.82.180] ([74.125.82.180:46685] helo=mail-we0-f180.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 7F/84-32208-508E2055 for ; Fri, 13 Mar 2015 08:37:10 -0500 Received: by wesk11 with SMTP id k11so23323896wes.13 for ; Fri, 13 Mar 2015 06:37:05 -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=mWPA/gtGEZNmzv727N27vldxS7y/kXhhmznBmvnkm/8=; b=nStqv3pXZSqA8R7z1glyBA8Yte/e0/xs4LO1cvlejkk9++U2sZcpxQGj+JbMD35C7A oqBpRhPgT7QU1UAthj6wGvOJv7lpCQU3UwsbwJXfIJN7SQzKHfFr5FVLq/fouIL5J31r +Y0Haz/3Z195Xc7QfepQrB+oL98qQ724/nzW8JsMpyFTIL2qSSzc4GdBHYY7NPxFPmUh /WjH4jV+Ui+5KvZl5jYMS5Jkf6eOhNTTmSH9naLKs6kCPZexEdOKqEsc5t2f8kLJj1Eb UX+DpG1juLT8/ujRKXMLjCmp66ZiY0VZGOuH7hkACbb8xTkUZj8TVInVkNMSSI95b2MG sxAA== X-Received: by 10.180.83.102 with SMTP id p6mr116558525wiy.78.1426253825229; Fri, 13 Mar 2015 06:37:05 -0700 (PDT) Received: from [192.168.0.159] ([62.189.198.114]) by mx.google.com with ESMTPSA id n6sm2872117wjy.8.2015.03.13.06.37.03 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 13 Mar 2015 06:37:03 -0700 (PDT) Message-ID: <5502E7D6.50909@gmail.com> Date: Fri, 13 Mar 2015 13:36:22 +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> <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> <98.26.24603.C9DE1055@pb1.pair.com> <5501F823.3060805@gmail.com> <55023052.40100@gmx.de> <7B.3E.24603.E83B2055@pb1.pair.com> <5502CFB4.7080406@gmail.com> In-Reply-To: <5502CFB4.7080406@gmail.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) Sorry, replying to myself to add a couple of thoughts / clarifications: Rowan Collins wrote on 13/03/2015 11:53: > Johannes Ott wrote on 13/03/2015 09:53: >> Why are in your opinion static members are not allowed to hold more >> complexe datastructures then simple scalars? > > Complex data structures, yes (and I see the use of __static in such > cases). Architectural logic gluing one class to another (as in your > Logger example), no. I think I've come up with a better distinction: *state* belongs in instances, whereas *data* can exist globally. A logger object encapsulates state, so a pointer to one is stateful information; storing that state statically is what feels wrong. A singleton in this case says "there is a bunch of stateful information here, but everywhere *in the current code* needs the same instance of it". >> Please give me one valid >> argument why I should double all of the functions in my code for a >> static and non-static access beside the argument that I "have to" use a >> Singleton. > > You don't need to double all the calls. In fact, the majority of your > code should not even know the Singleton is there, because they should > be treating it as though they have an instance. At certain key points > in your code (constructors, the top of methods) you add $foo = > Foo::getInstance();, then all your calls look like $foo->doBar(); > > That's the way I do it, anyway - treat getting the instance for a > singleton like importing a variable from global scope, which is > basically what it's doing. In other words, saying "Foo::getInstance()->bar();" is like using "$GLOBALS['foo']->bar();" saying "$foo = Foo::getInstance(); $foo->bar();" is like saying "global $foo; $foo->bar();" The act of retrieving the singleton is separate from the act of using it. Regards, -- Rowan Collins [IMSoP]