|
Posted by Steve on 10/12/06 14:46
"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) and you're using php < 5 (byval not byref) and you're calling
dosomething() within a loop. ;^) 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]
|