Re: Static constructor support

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

 



2012/9/26 Stuart Dallas <stuart@xxxxxxxx>

> On 26 Sep 2012, at 22:29, Yves Goergen <nospam.list@xxxxxxxxxxxxxxx>
> wrote:
>
> > On 26.09.2012 23:20 CE(S)T, Stuart Dallas wrote:
> >> If you mean what C# calls a static constructor, no that does not
> >> exist in PHP, but you can fake it.
> >
> > Okay, thank you for the quick info.
> >
> > How do other languages than C# call that? :-)
>
> They generally don't. C# is the only language I've ever come across that
> support such a thing, and I only found that by accident because it would
> never occur to me to look for it.
>

In java it's called a "static block" [1]

public class myclass{
    static{
        //some statements here
    }
}

And a use-case is pseudo-constant expression like

public class myclass{
    public static $CONSTANT;
    static{
        self::$CONSTANT = new DefaultFooBar;
    }
}



[1] http://www.erpgreat.com/java/use-of-a-static-block-in-a-class.htm

>
> > My class is a debug helper class, that can write trace messages and so
> > on. I have added a random per-request tag to distinguish concurrent
> > requests in the trace file and thought that generating such a tag would
> > perfectly fit in a static constructor. Now a helper function does that
> > check and generates one on the first call of the method.
>
>
> I would strongly recommend a singleton, or if you must use a static class
> you can either use the initialisation mechanism I described or, if the
> class has a single method as I'm guessing, have that method check the
> static variable to see if it's been set yet, and if not generate it before
> doing anything else.
>
> -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
>
>


-- 
github.com/KingCrunch

[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