Static Constructors

php.internals

Robert Silva

21 years ago
I've put together a rough implementation of static constructors. I'd like to get peoples thoughts about it then I'll either clean it up and submit it or nuke it. Basically the class constructor __cconstruct (have any better ideas?) is called upon the first access to any static property and marks the class entry as initialized. <? class Logger { public static $logfile = null; private static function __cconstruct() { self::$logfile = fopen('log.txt', 'a'); } public static function WriteLine($value) { $value .= PHP_EOL; fputs(self::$logfile, $value, strlen($value)); } } Logger::WriteLine('Static constructors'); ?>

Noah Botimer

21 years ago
Hi Robert, I hate to sound like a pedant, but shouldn't anything like that be explicit? That is, do your own check for prerequisite class variables that need to be set in each static method? Maybe I'm missing the boat, but I'd say that it would be something that should be at the application level, rather than at the system/object-model level. In your example: ... public static function WriteLine($value) { if (is_null(self::$logfile)) self::initialize(); ... } public static initialize() { self::$logfile = fopen('log.txt', 'a'); } ... Thanks, -Noah Robert Silva wrote:

Robert Silva

21 years ago
Well, its at the application level now because the language doesn't provide another alternative. Preferably, it would be at the system level. Your argument would be similar to saying that __construct should be explicit and called at the application level to initialize an object. Static classes may need initilization as well. This is modeled after C#'s static constructors and Java's static blocks. It also enables other classes to access complex static variables directly without going through a static method to initialize the property first. I agree that its not needed, but now that we have static class methods, it would be nice for the language to provide built in initialization facilities. Bob -----Original Message----- From: Noah Botimer [mailto:php@botimer.net] Sent: Tuesday, October 05, 2004 4:57 PM To: Robert Silva Cc: internals@lists.php.net Subject: Re: [PHP-DEV] Static Constructors Hi Robert, I hate to sound like a pedant, but shouldn't anything like that be explicit? That is, do your own check for prerequisite class variables that need to be set in each static method? Maybe I'm missing the boat, but I'd say that it would be something that should be at the application level, rather than at the system/object-model level. In your example: ... public static function WriteLine($value) { if (is_null(self::$logfile)) self::initialize(); ... } public static initialize() { self::$logfile = fopen('log.txt', 'a'); } ... Thanks, -Noah Robert Silva wrote:
> I've put together a rough implementation of static constructors. I'd like
to
> get peoples thoughts about it then I'll either clean it up and submit it
or
> nuke it. Basically the class constructor __cconstruct (have any better > ideas?) is called upon the first access to any static property and marks
the
> class entry as initialized. > > <? > > class Logger { > public static $logfile = null; > > private static function __cconstruct() { > self::$logfile = fopen('log.txt', 'a'); > } > public static function WriteLine($value) { > $value .= PHP_EOL; > fputs(self::$logfile, $value, strlen($value)); > } > > } > > Logger::WriteLine('Static constructors'); > > ?> >
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php

Andi Gutmans

21 years ago
I tend to think that static constructors are not a very important feature and therefore I wouldn't want to complicate the language. I don't see a problem with manually calling an initialization method in the case where static initialization isn't good enough. Note in many cases where statics are used (such as Singleton's) that extra if() can be done very nicely. Andi At 05:35 PM 10/5/2004 -0700, Robert Silva wrote:

Robert Silva

21 years ago
Well, I'll just chock it up to another learning experience although I disagree that it would complicate the language any more than __call, __get and __set already have. On a side note, here's a dumb question. I'm learning all this as I go and have never dealt with C or language compilers, the only part that is still a bit fuzzy is the structure of the opline/opcodes. What CS terminology would I want to research to gain a better understanding of how the opcodes are stored and processed. I've traced a running program step by step a few times now and haven't been able to follow what its doing with all the opline construction. I'd appreciate a pointer in the right direction. Also, are there some general guidelines posted anywhere for how & when to submit patches? Thanks Bob Silva -----Original Message----- From: Andi Gutmans [mailto:andi@zend.com] Sent: Tuesday, October 05, 2004 5:54 PM To: Robert Silva; internals@lists.php.net Subject: RE: [PHP-DEV] Static Constructors I tend to think that static constructors are not a very important feature and therefore I wouldn't want to complicate the language. I don't see a problem with manually calling an initialization method in the case where static initialization isn't good enough. Note in many cases where statics are used (such as Singleton's) that extra if() can be done very nicely. Andi At 05:35 PM 10/5/2004 -0700, Robert Silva wrote:
>Well, its at the application level now because the language doesn't provide >another alternative. Preferably, it would be at the system level. Your >argument would be similar to saying that __construct should be explicit and >called at the application level to initialize an object. Static classes may >need initilization as well. This is modeled after C#'s static constructors >and Java's static blocks. It also enables other classes to access complex >static variables directly without going through a static method to >initialize the property first. I agree that its not needed, but now that we >have static class methods, it would be nice for the language to provide >built in initialization facilities. > >Bob > > > >-----Original Message----- >From: Noah Botimer [mailto:php@botimer.net] >Sent: Tuesday, October 05, 2004 4:57 PM >To: Robert Silva >Cc: internals@lists.php.net >Subject: Re: [PHP-DEV] Static Constructors > >Hi Robert, > >I hate to sound like a pedant, but shouldn't anything like that be >explicit? That is, do your own check for prerequisite class variables >that need to be set in each static method? Maybe I'm missing the boat, >but I'd say that it would be something that should be at the application >level, rather than at the system/object-model level. > >In your example: > >... >public static function WriteLine($value) { > if (is_null(self::$logfile)) > self::initialize(); > ... >} > >public static initialize() { > self::$logfile = fopen('log.txt', 'a'); >} >... > >Thanks, >-Noah > > >Robert Silva wrote: > > > I've put together a rough implementation of static constructors. I'd
like

Wez Furlong

21 years ago
On Tue, 5 Oct 2004 18:58:26 -0700, Robert Silva <junkmail@bobsilva.com> wrote:
> Also, are there some general guidelines posted anywhere for how & when to > submit patches?
Look at the collection of README files in the php-src