Functions with Static Variables vs. Classes

This is a discussion on Functions with Static Variables vs. Classes within the PHP General forums, part of the PHP Programming Forums category; For some simple applications I use a function to collect values in a static variable and to return them when ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-29-2007
news_yodpeirs@thoftware.de
 
Posts: n/a
Default Functions with Static Variables vs. Classes

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();
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
Reply With Quote
  #2 (permalink)  
Old 11-29-2007
Zoltán Németh
 
Posts: n/a
Default Re: [PHP] Functions with Static Variables vs. Classes

2007. 11. 29, csütörtök keltezéssel 14.18-kor news_yodpeirs@thoftware.de
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
>

Reply With Quote
  #3 (permalink)  
Old 11-29-2007
news_yodpeirs@thoftware.de
 
Posts: n/a
Default Re: [PHP] Functions with Static Variables vs. Classes

From: "Zoltán Németh" <znemeth@alterationx.hu>
>> 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?


I think so - otherwise static Variables would be quite senseless. The line
starting with static is (so do I think) once evaluated at compile-time or at
the first run and the ignored.

Thomas
Reply With Quote
  #4 (permalink)  
Old 11-29-2007
Jochem Maas
 
Posts: n/a
Default Re: [PHP] Functions with Static Variables vs. Classes

news_yodpeirs@thoftware.de wrote:
> From: "Zoltán Németh" <znemeth@alterationx.hu>
>>> 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?


indeed you are :-)

>
> I think so - otherwise static Variables would be quite senseless. The line
> starting with static is (so do I think) once evaluated at compile-time or at
> the first run and the ignored.


I believe it's a compile time definition ... which is the reason you can only
initialize static vars with scalar values (and not the result of expressions or
resources or objects, etc)


>
> Thomas
>

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 03:49 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0