# Static Constructors

- Thread: https://externals.io/message/13183
- Original archive: https://news-web.php.net/php.internals/13183
- Started by Robert Silva on 2004-10-05T23:36:24+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #13183 — Robert Silva — 2004-10-05T23:36:24+00:00

URL: https://externals.io/message/13183#13183

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');

?

---

## #13184 — Noah Botimer — 2004-10-05T23:57:22+00:00

URL: https://externals.io/message/13183#13184 · In reply to: #13183

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');
> 
> ?
