Re: [PHP] Static constructor support
On Wed, Sep 26, 2012 at 2:29 PM, Yves Goergen wrote: > How do other languages than C# call that? :-) > Java has "static initializers" which work the same way: they are executed when the class is first loaded and before any code can make use of the class. David
Re: [PHP] Static constructor support
2012/9/26 Stuart Dallas > On 26 Sep 2012, at 22:29, Yves Goergen > 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
Re: [PHP] Static constructor support
2012/9/27 Yves Goergen > On 26.09.2012 23:38 CE(S)T, Stuart Dallas wrote: > > On 26 Sep 2012, at 22:29, Yves Goergen > > wrote: > >> 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. > > Why does everybody seem to recommend singletons so strongly? What's > wrong with static classes? Are they considered as "global variables", > the most evil remainder from the Middle Age? What is easier to call on a > regular hacky basis: > > XyzDebug::Trace(...); > > or > > XyzDebug::GetInstance()->Trace(...); > Or just XyzDebug\trace(); Functions are not dead yet > > I really prefer the first one. I always use static classes when there is > no real instance of something. Why should I force to act as if, only to > follow some trendy pattern? > > -- > Yves Goergen - nospam.l...@unclassified.de - http://unclassified.de > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- github.com/KingCrunch
Re: [PHP] Static constructor support
On 26.09.2012 23:38 CE(S)T, Stuart Dallas wrote: > On 26 Sep 2012, at 22:29, Yves Goergen > wrote: >> 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. Why does everybody seem to recommend singletons so strongly? What's wrong with static classes? Are they considered as "global variables", the most evil remainder from the Middle Age? What is easier to call on a regular hacky basis: XyzDebug::Trace(...); or XyzDebug::GetInstance()->Trace(...); I really prefer the first one. I always use static classes when there is no real instance of something. Why should I force to act as if, only to follow some trendy pattern? -- Yves Goergen - nospam.l...@unclassified.de - http://unclassified.de -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Static constructor support
On 26 Sep 2012, at 22:29, Yves Goergen 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. > 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
Re: [PHP] Static constructor support
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? :-) 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. -- Yves Goergen - nospam.l...@unclassified.de - http://unclassified.de -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Static constructor support
On 26 Sep 2012, at 22:13, Yves Goergen 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… http://3ft9.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php