Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:84943 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 88530 invoked from network); 15 Mar 2015 22:17:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Mar 2015 22:17:15 -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.173 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.173 mail-we0-f173.google.com Received: from [74.125.82.173] ([74.125.82.173:35951] helo=mail-we0-f173.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id BB/33-06614-7E406055 for ; Sun, 15 Mar 2015 17:17:12 -0500 Received: by wetk59 with SMTP id k59so26210506wet.3 for ; Sun, 15 Mar 2015 15:17:07 -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=+8RgQHBAbaffYGR1EcgcmwI9gCjHVQP5/1hPh8aqrEU=; b=ZGWH7tnh9Q4aZ+/sBN3sXwQD8Fc1sUcDLTXdKPFEiPw5Bc99EAOp9zQ3HCxfWgh6CR FjeuUONISb41Jzmqx6KXMM+UwNMrbqwUjaBh+mV7N2MFXGeftwwMdW0FpMY/9GvSBVxg xT/+s0uX8e6WquBv9RGXwOfQVyQqrJxnIWi8wuqrpE8bwLhyh3PpRBVWuts7YfNyy4GT dA+QUujZWldqN5JqOi0vbmYc9xltjEG4ur2vFv+ZOFNHBnVa7F5l6QTLwNvKgqJk3e1T xwPkyBjxD2Bludbt5JMX/BEXQjucsraEjoUeANUQhdybJqMiFUrlvJxMo7iwna1mfk7s kiTw== X-Received: by 10.194.6.70 with SMTP id y6mr114166280wjy.97.1426457826987; Sun, 15 Mar 2015 15:17:06 -0700 (PDT) Received: from [192.168.0.5] (cpc68956-brig15-2-0-cust215.3-3.cable.virginm.net. [82.6.24.216]) by mx.google.com with ESMTPSA id lb6sm12652186wjb.22.2015.03.15.15.17.06 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 15 Mar 2015 15:17:06 -0700 (PDT) Message-ID: <550604E1.9040506@gmail.com> Date: Sun, 15 Mar 2015 22:17:05 +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> <55028793.5040606@googlemail.com> <04.FE.24603.D5CB2055@pb1.pair.com> <5503D9E8.1090008@googlemail.com> <57.60.52108.BE854055@pb1.pair.com> <5504712D.4010103@googlemail.com> <55049B17.2060000@deroetzi.de> <550558AB.9010402@googlemail.com> <5505D3D4.2040006@gmail.com> In-Reply-To: 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) On 15/03/2015 20:52, Johannes Ott wrote: > Am 15.03.2015 um 19:47 schrieb Rowan Collins: >> On 15/03/2015 10:41, Johannes Ott wrote: >>> Okay get your point, but as already discussed several times, the rfc >>> should not be declined for the reason a ppl, who doesn't understand when >>> to use static context or when not to use at all, can do crucial things. >>> Because he although can do without the static constructor. >>> >>> For a horiffic example: >>> >>> class Example { >>> >>> private static $handler; >>> >>> public function open() { >>> self::$handler = fopen('example.txt'); >>> } >>> >>> ... >>> >>> } >>> >>> Example::open(); >>> >>> Indeed I have the opinion some beginner who is doing such horiffic code >>> maybe think more about what he is doing and especially about the side >>> effects inside a so called "magic" method, then outside. >> I'm not clear what's so different between this and your logger example. >> Here you have a file handle, which in PHP happens to be a special type >> rather than an object, and are storing it in a static property; closing >> the file handle has to be managed somehow, and this code is letting PHP >> do this implicitly with what amounts to a destructor on the file handle >> resource. >> > The difference is as I told the Resource Handler is wrapped inside a > Singleton Instance with a destructor! Whether the instance is a singleton, or has a non-default destructor, makes no difference - the cleanup happens during PHP's shutdown procedure. As it happens, resources have the equivalent of destructors internally, so even the object instance vs resource doesn't actually matter here. >> Or, you can *invalidate* the objects, rather than destructing them, as >> implied by your "DBAdapter::closeAllConnections()" example - there will >> still be references to those connections live in your code if, for >> instance, you have Example::$logger, and $logger->dbConnection. You >> can't have an Example::cleanupResources() method, because it would fire >> the static constructor on pages which hadn't used the class yet, >> destroying the lazy-loading. > You always talking about the singleton-pattern, as I although told > different times now, I have no resources directly stored in static > context but in singleton instances inside. > > for example the DBAdapter::closeAllConnections(): > > public static closeAllConnections() { > while(count(self::$connections) > 0) { > self::$connections[0]->close(); > unset(self::$connections[0]); > } > } > > the same for LogAdapter. This doesn't destroy those objects - PHP is a purely garbage collected language (in the sense of no manually managed memory / resources), so you can't destroy an object something still points to. If you gave out a reference to a DB connection and stored it in a static variable somewhere, that reference still ponts to the object, and you will have to detect that it is a closed connection and handle it appropriately. This isn't to do with public constructors vs singletons, or even objects vs resources, it's about the handle being shared amongst multiple users, no one of which can unilaterally "unshare" it. My point about lazy-loading is that if you have a class like this... class LazyThing { private static $foo; private function __static() { self::$foo = Something::GetSomeKindOfWrappedResource(); } public function cleanup() { self::$foo->destroy(); } } ...and then don't need it on a particular page, when you call LazyThing::cleanup(), it will actually trigger the static constructor, potentially triggering a whole chain of resources to be opened just in order to close them again. If that's the case, then you might as well have a matching pair of Foo::initialise() and Foo::cleanup() calls which are both called explicitly. So explicit cleanup routines can't usefully be used in the same class as implicit initialisation, which kind of makes sense, but rules them out as an alternative to static destructors. Regards, -- Rowan Collins [IMSoP]