|
Posted by Jim Carlock on 12/26/06 00:29
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 Carlock
Post replies to the group.
Navigation:
[Reply to this message]
|