Page 1 of 1

Global Variables

Posted: Fri Feb 03, 2012 1:17 am
by BobN
Okay, I've read several pages about variables and their scope but I can't believe I have actually read everything on the subject - surely I've missed something.

Issue - global variables available in in functions in an "include" file.

Is it true that PHP has no way of specifying that a variable is globals in scope with out messing around with GLOBAL on references to a global variable or using $GLOBALS every time your reference them.

Is it true, there is no way to define a variable, set a value, and declare it as a global variable in one or more statements of code?

If it is true, who the heck designed this language?

I've been programming computers (with about a dozen languages and semi-languages) for 37+ years and this is the first language I've come across which supports global variables but makes referencing them so verbose.

It seems ass-backwards to declare that a variable is global INSIDE a function. Why not define it as Global when you define the variable and set the value?

Heck, even JavaScript, a relatively simple language, provides a quick and easy method of defining globals - you define them outside of functions.

Doesn't PHP have something similar?

Bob

Re: Global Variables

Posted: Fri Feb 03, 2012 1:38 am
by Christopher
You are misunderstanding globals in PHP. Like other languages, a variable declared outside of all functions and classes is in the global namespace. The difference is that in many languages global variables are automatically visible inside functions. In PHP you must explicitly declare them to make them visible with the global keyword.

Code: Select all

$foo = 'something';     // this is a global variable

function one() {
     // $foo is NOT visible here
}

function two() {
    global $foo;
     // $foo IS visible here
}
PS - Whether you dislike this syntax is irrelevant, this is how it works in PHP.

PPS - You shouldn't use global variables in this way anyway. Instead declare as few variables in the global namespace as possible and always inject variables into functions and objects.

Re: Global Variables

Posted: Fri Feb 03, 2012 2:16 am
by BobN
So as not to misunderstand the way PHP does it, compared to other languages:

What, presicely, do you mean when you say
. . . inject variables into functions and objects.

Re: Global Variables

Posted: Fri Feb 03, 2012 5:04 am
by Riyaverma
Thanks for sharing the this helpful post.I am facing the same problem.
I confuse how we manage the global varible through the program and how we call it in throughout the program.

Re: Global Variables

Posted: Fri Feb 03, 2012 6:33 pm
by Christopher
By injecting, I mean:

Code: Select all

$foo = 'something';     // this is a global variable

function one($foo) {
     // $foo IS visible here - GOOD
}

function two() {
    global $foo;
     // $foo IS visible here - BAD
}
With function one() you can follow the variable $foo down through calls to see where it is used. With the global some totally unrelated code can change the global $foo without you knowing where it happened. These are called side-effects and code is more maintainable without them.

Re: Global Variables

Posted: Fri Feb 03, 2012 9:16 pm
by BobN
How about good old OOP - "encapsulating the data with the function" (OOP is a bit overblown in my opinion)?

Code: Select all

<?php

function getSetValue($action, $newValue = NULL){
     STATIC $statVariable;
     switch (strtolower($action)) {
          case "set";
               if ($newValue === NULL) {
                    echo "<br>You did not pass a new value on a GET call";
                    return false;
               }
               $statVariable = $newValue;
               return true;
               break;
          case "get";
               return $statVariable;
               break;
          default:
               echo "<br>You passed an invalid action '" . $action . "'";
               return false;
               break;
     };
};
function main() {
     $br = "<br>";
     getSetValue("ssss",20);
     getSetValue("set");
     getSetValue("gggg");
     getSetValue("set",20);
     echo $br . "Should be 20: " . getSetValue("get");
     getSetValue("set",45);
     echo $br . "Should be 45: " . getSetValue("get");
     getSetValue("set",101);
     echo $br . "Should be 101: " . getSetValue("get");
     getSetValue("set",-1);
     echo $br . "Should be -1: " . getSetValue("get");
     getSetValue("set",false);
     echo $br . "Should be null: '" . getSetValue("set") . "'";
};

main();
?>

Re: Global Variables

Posted: Sat Feb 04, 2012 1:37 am
by Christopher
BobN wrote:How about good old OOP - "encapsulating the data with the function"
What you show is not OOP ... maybe it is "good old OOP"? It is important to note that one of the most important things about OOP is that is provides a solution to exactly the problem we are discussing. An object is a private namespace that contains variables and the functions that can operate on those variables. This helps reduce side-effects.
BobN wrote:(OOP is a bit overblown in my opinion)?
I am not sure how the methodology as ordinary as OOP can be called "overblown"? But I suppose that's what Assembly programmers say about block-structured languages!