Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:84869 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 64295 invoked from network); 15 Mar 2015 18:47:53 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Mar 2015 18:47:53 -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 209.85.212.175 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.175 mail-wi0-f175.google.com Received: from [209.85.212.175] ([209.85.212.175:34685] helo=mail-wi0-f175.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 01/A8-29489-8D3D5055 for ; Sun, 15 Mar 2015 13:47:52 -0500 Received: by wibg7 with SMTP id g7so18924699wib.1 for ; Sun, 15 Mar 2015 11:47:50 -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=DtxUk+uoTDtUg4wP5pVfx5y+oITIGE+AJgXzbRfwLM8=; b=KZv/BHCcYiKU13IXtVnhFa7yAqNV8JgghDMJfpKsmGwGj6qf8W6CqvdQbvKW8qGYJS bcMJb1E9CEcWbhMpsSg7tQAw5QkRGpXlxruNquNb4HNWA8LW+1CF3ThvfXF6Hd8qODmA I140n6ziGUARJ1UXAp2OIfRS5PFkZMsSUJimw+vcqnapP7gAZCgI37taOy1BtWuim8KO dYfr8JMAW3fQZgijwV+XXsnPG3ev58oJaNVeLiRo4VvoKd/Aqo1jCrQ4klGcHjH7nPEi uLuSDD0vEq99ODx6ADAHRNP1FLDHrwmSGEdNnkuoWMRFx9VwoX33CIPhQZjHTMTUQDvJ 1NiQ== X-Received: by 10.180.77.199 with SMTP id u7mr54671606wiw.42.1426445270100; Sun, 15 Mar 2015 11:47:50 -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 dn7sm11999282wid.12.2015.03.15.11.47.49 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 15 Mar 2015 11:47:49 -0700 (PDT) Message-ID: <5505D3D4.2040006@gmail.com> Date: Sun, 15 Mar 2015 18:47:48 +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> 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 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. > I never ever would store any kind of resources (opening any kind of > connections to db, file, socket etc.) directly to the static context > without wrapping in a instance, because those are really dynamic handles > which need a proper constructor and destructor for the special connection. However many intermediate instances you create, at some point the destructor has to run, and that will only happen once the static reference is unset. Luckily, the Zend Engine will go through and garbage collect all global and static variables at the end of a request, so you can cheat that way. 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. What you're really relying on is that the implicit static destructor provided by the engine is good enough for the kinds of resource which a static constructor should be dealing with. This is also true of many objects, which don't technically need a custom destructor to close file or DB handles, because Zend will reference count the resource, but more advanced objects can do more advanced cleanup. Regards, -- Rowan Collins [IMSoP]