Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:84638 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 75991 invoked from network); 12 Mar 2015 14:51:17 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Mar 2015 14:51:17 -0000 X-Host-Fingerprint: 194.166.179.101 194-166-179-101.adsl.highway.telekom.at Received: from [194.166.179.101] ([194.166.179.101:19900] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 3D/85-42021-3E7A1055 for ; Thu, 12 Mar 2015 09:51:16 -0500 Message-ID: <3D.85.42021.3E7A1055@pb1.pair.com> To: internals@lists.php.net Date: Thu, 12 Mar 2015 15:51:11 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 References: <6D.2C.32765.10EC0055@pb1.pair.com> <5500D967.5040800@gmail.com> <13.69.64353.73451055@pb1.pair.com> <5501876C.3020107@gmail.com> In-Reply-To: <5501876C.3020107@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Posted-By: 194.166.179.101 Subject: Re: [PHP-DEV] static constructor From: mail@deroetzi.de (Johannes Ott) > > 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. > I really disagree to this. Singletons are a typical FactoryPattern, none of the examples except the LogAdapter itself are doing implementation of a factory. They are just initialize some computed static variables once in the live time of the class mostly for internal usage of the class. That is nearly like initializing a class constant, but in my opinion a constant should not have a "complex" algorithm (For example conditions or read from filesystem). That should be encapsulated inside a proper method body. In use case 1 Dependency Injection maybe another solution but does not exactly do what I want to do. > >> 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. > yes that would be another valid pattern. And would be prefered for me if I use the Logger in 1 or 2 out of 10 class methods, which are not used very often. But mostly I'm using it in 9 or 10 of 10 methods, which are invoked several times through lifetime. So doing a null check each time is a overhead of calculation which can be avoided with this static constructor pattern. > > 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. I agree the real creation(/parsing) of the class should have no side-effects. But the static constructor is not executed at the creation-time of the class but directly before first access on the class. That are two totally different moments in the lifecycle of the class and does not block the possibility to first read all classes without any side-effects. > 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. > The static constructor or sometimes although called class constructor is a well known OOP-pattern implemented by different languages like C# or Java. Regards, -- DerOetzi