Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:84616 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 21119 invoked from network); 12 Mar 2015 08:54:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Mar 2015 08:54:16 -0000 X-Host-Fingerprint: 91.115.245.126 91-115-245-126.adsl.highway.telekom.at Received: from [91.115.245.126] ([91.115.245.126:26108] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 13/69-64353-73451055 for ; Thu, 12 Mar 2015 03:54:16 -0500 Message-ID: <13.69.64353.73451055@pb1.pair.com> To: internals@lists.php.net Date: Thu, 12 Mar 2015 09:54:12 +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> In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Posted-By: 91.115.245.126 Subject: Re: [PHP-DEV] static constructor From: mail@deroetzi.de (Johannes Ott) Am 12.03.2015 um 05:17 schrieb Levi Morrison: > On Wed, Mar 11, 2015 at 6:10 PM, Rowan Collins wrote: >> On 11/03/2015 23:21, Johannes Ott wrote: >>> >>> So now I want to do my first own proposal for a new function in PHP and >>> I hope doing it right with starting a discussion here first. >>> >>> The purpose of this suggestion is to introduce a static constructor, >>> which is called before the first call to class either static or >>> non-static to initialize some static properties which are needed by the >>> class. >> >> >> Can you give an example use case for when this would be useful? I'm >> struggling to think of one for which there isn't already an established >> coding pattern... > > Notably, user-land enums. You don't want public constructors because > you don't want it instantiated except for each enum property. You also > need it to run on class creation, not afterwards. > > I think we'd be better off adding language-level enums than static > constructors though. > Yes indeed user-land enums are one of the use cases I use this feature at the moment. But there some other use cases as well: 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. 2. Some Config classes which are intelligent wrapper-classes to some ini-files and database have to read in some things first. class Config { private static $ini; public static function __static() { self::$ini = parse_ini_file(...); // or // Read from Database } } Config::__static(); 3. For a multilanguage system I determine the user language by useragent and fallback to default-language if user language ist not supported. class Text { private static $defaultLang; private static $userLang; public static function __static() { self::$defaultLang = //read from Database self::setUserLanguageOrFallback(); } private static function setUserLanguageOrFallback() { ... } } Text::__static(); 4. Already prepare some PDO-statements which needed all over the class class Example { private static $stmts = []; public static function __static() { self::$stmts['select1'] = new DBSelect('SELECT * FROM table WHERE `col1`=:clause'); } } Example::__static(); That are the examples I can found on a quick search over my code. Regards, -- DerOetzi