You are here: Re: use a global or pass a variable « All PHP « IT news, forums, messages
Re: use a global or pass a variable

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация