Re: Static constructor support
| From: | Stuart Dallas | Date: | Wed, 26 Sep 2012 21:20:51 +0000 |
| Subject: | Re: Static constructor support | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-319269@lists.php.net to get a copy of this message | ||
On 26 Sep 2012, at 22:13, Yves Goergen <nospam.list@unclassified.de> wrote:
> I couldn't find out whether PHP supports static constructors, and how
> the syntax is. The web and the PHP manual don't mention it. So is it not
> supported? If it is, is there a PHP version restriction?
If you mean what C# calls a static constructor, no that does not exist in PHP, but you can fake it.
Make sure the class is in it's own file, and you can initialise it like so…
<?php
MyStaticClass::init();
class MyStaticClass
{
static public function init()
{
// Do initialisation here
}
}
Then, when the class file is required the initialisation method will automatically be executed.
However, I wouldn't encourage you to use static classes like this. The singleton pattern would
be my recommendation.
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/