Re: Static constructor support

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 26 Sep 2012, at 22:13, Yves Goergen <nospam.list@xxxxxxxxxxxxxxx> 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/
-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux