php-general Digest 27 Sep 2012 09:31:36 -0000 Issue 7982
Topics (messages 319268 through 319275):
Static constructor support
319268 by: Yves Goergen
319269 by: Stuart Dallas
319270 by: Yves Goergen
319271 by: Stuart Dallas
319272 by: Yves Goergen
319273 by: Sebastian Krebs
319274 by: Sebastian Krebs
about lock some codes.
319275 by: lx
Administrivia:
To subscribe to the digest, e-mail:
php-general-digest-subscr...@lists.php.net
To unsubscribe from the digest, e-mail:
php-general-digest-unsubscr...@lists.php.net
To post to the list, e-mail:
php-gene...@lists.php.net
----------------------------------------------------------------------
--- Begin Message ---
Hi,
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?
--
Yves Goergen - nospam.l...@unclassified.de - http://unclassified.de
--- End Message ---
--- Begin Message ---
On 26 Sep 2012, at 22:13, Yves Goergen <nospam.l...@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/
--- End Message ---
--- Begin Message ---
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
--- End Message ---
--- Begin Message ---
On 26 Sep 2012, at 22:29, Yves Goergen <nospam.l...@unclassified.de> 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/
--- End Message ---
--- Begin Message ---
On 26.09.2012 23:38 CE(S)T, Stuart Dallas wrote:
> On 26 Sep 2012, at 22:29, Yves Goergen <nospam.l...@unclassified.de>
> 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
--- End Message ---
--- Begin Message ---
2012/9/27 Yves Goergen <nospam.l...@unclassified.de>
> On 26.09.2012 23:38 CE(S)T, Stuart Dallas wrote:
> > On 26 Sep 2012, at 22:29, Yves Goergen <nospam.l...@unclassified.de>
> > 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
--- End Message ---
--- Begin Message ---
2012/9/26 Stuart Dallas <stu...@3ft9.com>
> On 26 Sep 2012, at 22:29, Yves Goergen <nospam.l...@unclassified.de>
> 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
--- End Message ---
--- Begin Message ---
Hello:
I have a question now.the code is:
$ftemp = fopen("$fdoc_tmp/temp_proxy", 'w');
fwrite($ftemp, $content);
fclose($ftemp);
exec("/usr/local/bin/gdnsproxy -a -f $fdoc_tmp/temp_proxy
1>$fdoc_tmp/dout 2>$fdoc_tmp/derr", $data, $ex_result);
echo "ret=".$ex_result;
As you see, If a process run these,this is right.But two processes run this
codes.
the order maybe is:
1.process 1 create the temp_proxy1
2.process 2 create the temp_proxy2
3.process 1 exec temp_proxy2
4.process 2 exec temp_proxy2
The temp_proxy1 can't exec.
So,I want to know how to solve it.
Thank you.
--- End Message ---