|
Posted by windandwaves on 10/12/06 23:48
Steve wrote:
> "ZeldorBlat" <zeldorblat@gmail.com> wrote in message
> news:1160656554.301340.255580@m7g2000cwm.googlegroups.com...
> |
> | windandwaves wrote:
> | > Hi Folk
> | >
> | > Consider the two code options below. I want to know if it makes more
> | > sense, in theory, to pass a variable or to make it global
> | >
> | > FIRST OPTION --
> | >
> | > ...
> | >
> | > $data = 5;
> | >
> | > $data = dosomething($data)
> | >
> | > ...
> | >
> | > echo $data;
> | > exit();
> | >
> | > function dosomething($data) {
> | > $myval = 3;
> | > $data = $data + $myval;
> | > return $data;
> | > }
> | >
> | > SECOND OPTION --
> | > ...
> | >
> | > $data = 5;
> | >
> | > dosomething()
> | >
> | > ...
> | >
> | > echo $data;
> | > exit();
> | >
> | > function dosomething() {
> | > global $data;
> | > $myval = 3;
> | > $data = $data + $myval;
> | > }
> |
> | First option -- always, always, always.
>
> unless...$data is a huge representation of information in memory (which you
> are short on)
true
> and you're using php < 5
true
>(byval not byref)
Not sure
> and you're calling
> dosomething() within a loop. ;^)
false....
Hmmm, I was close.
My thinking indeed was that if the data was huge and it had to be
thrown into the function and then back it would make more sense to keep
it were it was and edit it a little in the function....
> but as i use english, always means
> sometimes and always, always means rarely and always, always, always means
> hardly ever. first option is the more preferable method as it helps decouple
> that function and it's context...which is what you want.
>
> however in OO, creating static class interfaces is essentially the same as
> 'global $data'. it's just that you have a framework in place that formalizes
> the programmatic meaning of $data.
Navigation:
[Reply to this message]
|