You are here: Re: Exposing Variables « PHP Programming Language « IT news, forums, messages
Re: Exposing Variables

Posted by Jerry Stuckle on 12/27/06 01:34

Jim Carlock wrote:
> I've been using global to expose variables. However, I noticed
> the following...
>
> (1) global seems to use the nearest declaration.
>
> For example,
>
> // module1.php
> $sMyString = "Hello World.";
>
> function Function1($sItemToProcess1) {
> global $ALL_GETS, $sMyString;
> // Above references $ALL_GETS in a different module.
> // ...
> // code to process some things
> $sItemToProcess2 = $sItemToProcess1 . " " . $sMyString;
> // pass in the new variable to the second function...
> $sCheckThis = Function2($sItemToProcess2)
> }
>
> function Function2($sItemToProcess) {
> global $ALL_GETS; // this one references $ALL_GETS in...
> // Function1(), not $ALL_GETS in the originating module...
>
> // I used different variable names when I noticed that global
> // also references variables defined in a calling procedure.
> //
> // PHP 4.4.4 (cgi-fcgi) (built: Aug 16 2006 01:17:43)
> //
>
> // I accidently discovered this while creating a variable inside
> // one function and was thinking that I'd need to put it outside
> // the function and use the global keyword to make it visible.
> //
> // However, PHP seems to allow creation inside one function,
> // then makes that creation visible via the global keyword in
> // another function.
> //
>
> // ... code to process things ...
> }
>
> Meanwhile, both of the functions retain visibility (but not so for the
> variables declared in those included/required modules), e.g...
>
> // module2.php
> require("/include/module1.php");
>
> $ALL_GETS = array_change_key_case($_GET, CASE_LOWER);
> $sThisItem = "blahblah_example";
> $sThisItem2 = Function1($sThisItem);
>
> I'm seeing that I can use a function name to return a variable and
> make the variable visible...
>
> // module3.php
> $sThisIsPrivateTag = "This is sThisIsPrivateTag.";
>
> function PrivateTagGetContents() {
> return($sThisIsPrivateTag);
> }
>
> function PrivateTagUpdateContents($sUpdate) {
> $sThisIsPrivateTag = $sUpdate;
> // return($sThisIsPrivateTag);
> }
>
> That in effect gets rid of the need for using the global keyword.
>
> I haven't dug into object use inside of PHP and I'm looking for
> alternatives to using global.
>
> I appreciate any comments/suggestions/alternatives.
>
> Thanks.
>

Jim,

It's better not to use globals. Rather, pass the needed values in the
function call. Much better programming practice.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

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

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