Am 12.03.2015 um 05:17 schrieb Levi Morrison: > On Wed, Mar 11, 2015 at 6:10 PM, Rowan Collins <rowan.coll...@gmail.com> > 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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php