Re: [PHP] Functions with Static Variables vs. Classes
- From: znemeth@xxxxxxxxxxxxxx (Zoltán Németh)
- Date: Thu, 29 Nov 2007 14:26:54 +0100
2007. 11. 29, csütörtök keltezéssel 14.18-kor news_yodpeirs@xxxxxxxxxxxx
ezt írta:
For some simple applications I use a function to collect values in a static
variable and to return them when called in a special way, just like this
(fairly senseless) example:
function example($elem='') {
static $store = array();
AFAIK the above line should cause an error on the second run of the
function, as you declare the same static variable for the second time.
or am I wrong?
greets
Zoltán Németh
if (!func_num_args()) return($store);.
... do something with $elem ...
$store[] = $elem;
}
I would call this a singleton-micro-class, as it works like a class with
data and methods, but there is always only one of it, having only one
method.
Why do I? Because I dont need to worry about variablescope as if I would use
global variables and I dont have to initialize an object before the first
call (with the scope-problem again). I simply can call it everywhere and
everytime.
Do you have any comments to this approach?
Thomas
- Follow-Ups:
- Re: [PHP] Functions with Static Variables vs. Classes
- From: news_yodpeirs
- Re: [PHP] Functions with Static Variables vs. Classes
- References:
- Functions with Static Variables vs. Classes
- From: news_yodpeirs
- Functions with Static Variables vs. Classes
- Prev by Date: Re: [PHP] Structured Code vs. Performance
- Next by Date: Re: [PHP] Structured Code vs. Performance
- Previous by thread: Functions with Static Variables vs. Classes
- Next by thread: Re: [PHP] Functions with Static Variables vs. Classes
- Index(es):