Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: PHP Global Object

  1. #1
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default PHP Global Object

    Is there a name for the global object in PHP?

    Like in javascript, if you are outside of all functions, all five of these are equivalent (though the last four are less prone to lead to errors later):

    Code:
    bark = 'woof';
    
    var bark = 'woof';
    
    window.bark = 'woof';
    
    self.bark = 'woof';
    
    this.bark = 'woof';
    The last three of these all use javascript's obj.propertyName convention, and in this case this, window, and self all refer to the global object (at least in a browser environment).

    Is there one or more keyword(s) that do this in PHP. I know it cannot be window, because there is no window on the server. And this appears in limited testing to be meaningless in PHP's global scope.

    I know there must be a PHP global object. Otherwise:

    PHP Code:
    $myVar 'bob'
    and really all PHP code wouldn't do anything. But it's possible that there is no name/keyword for the global object PHP.

    Anyone know for sure on this one?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Edit:

    see my example below. this post was confusing.

    no, there isn't really such a keyword. basically, everything is in the global scope unless it's in some other scope

    $this only works from within a class (object).

    so, whatever scope you're in when you assign a variable is the scope it's available in. AFAIK, you can't assign a variable to a particular scope while you're inside another. In the global scope (outside all functions, classes, namespaces), $myVar = 'bob'; creates a global variable. Inside a function/class/namespace, it creates a local variable. If you're inside a function and you want to reference a variable in the global scope, you can use $GLOBALS['myVar'] (references $myVar) or declare global $myVar; (from inside the local scope; makes $myVar available in the current, local scope).

    kinda counter-intuitive, IMHO... I would have made it so you could create a variable in the global scope, declare it global, and then have it automatically available in all scopes... but oh well. it is what it is. that's basically what $GLOBALS does, I guess.
    Last edited by traq; 07-05-2010 at 11:40 PM.

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Can you give me an example of using a variable defined outside a function in a function without passing it to the function?

    Or can you give me an example of defining a global variable inside a function and then later accessing it outside that function?

    Are there any of either?

    Your post tends to imply that there are (perhaps only one or the other), but doesn't show any clear way in which to do either one.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    hmm...
    PHP Code:
    <?php

    $myVar 
    'global scope';

    function 
    using_GLOBALS(){
      
    $myVar 'local scope';
      echo 
    $myVar;  //  outputs 'local scope'
      
    echo $GLOBALS['myVar'];  //  outputs 'global scope'
    }

    // OR

    function using_global(){
      echo 
    $myVar;  //  outputs nothing
      
    global $myVar;
      echo 
    $myVar;  //  outputs 'global scope'
    }

    ?>
    as far as defining a global variable from inside a local scope, I'm not sure it can be done. the best thing I can think of is:
    PHP Code:
    <?php

    function return_global(){
       
    $var 'value';
       return 
    $var;
    }

    $myVar return_global();

    ?>
    better explanation than mine: variable scope

    Edit:

    equally ugly, but this works too, and it saves the return for more important stuff:
    PHP Code:
    <?php

    function make_global(){
        
    $mylocalvar 'I was born local';
        
    $GLOBALS['myVar'] = $mylocalvar;
    }

    make_global();
    echo 
    $GLOBALS['myVar'];

    //  you could, obviously, use $_SESSION, $_ENV, whatever, instead.
    //  but, despite the fact that it works, 
    //  I don't think any of the superglobals were actually intended for this use -
    //  so it might cause unforeseen problems.

    ?>
    As far as I can tell, all the "global" methods seem aimed at bringing global things into the local scope, and not vice-versa.
    The confusing thing (for me, at least) is that global things aren't available locally - not by default.

    Last edited by traq; 07-05-2010 at 11:45 PM.

  5. The Following User Says Thank You to traq For This Useful Post:

    jscheuer1 (07-06-2010)

  6. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    traq has answered everything in detail.

    Here's a summary and a couple more things:

    "global $var;" anywhere will force a variable to be global.
    "$var;" is irrelevant and you should just skip to using the variable. If you must preset a variable to avoid referring to an empty variable, then give it a value, such as FALSE, 0 or ''. Frequently $ar = array(); is helpful to avoid looping through possibly empty arrays.

    $GLOBALS['var'] is something I recently discovered and I love it. This allows clarity while using the variable rather than sometimes sharing globals and other times not.

    Objects are just variables and work like any other.
    $obj = new Obj;
    Then you use it like anything else.
    $obj->var refers to a variable inside the object (similar to an array, but complex).
    $this->var refers to that same variable, but only within the class (and subclass??).

    Aside from those, I'm not aware of anything similar to the javascript "."/dot method. In PHP, there's -> for classes and of course arrays where you can get $var['one']['two'], so somtimes that can kinda be similar to your window.var example, but not in that sense.

    The main difference in PHP and JS is that you never need to declare a variable to use it (I'm not even sure if you should/can). Just declare a variable as global if needed, or I'd now recommend using $GLOBALS.


    One last thing: there are a few special "superglobals" that never change scope. Thus you can always call $_GET, and other vars with the underscore anywhere. (Unfortunately you can't make up your own like $_MYVARS.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. The Following User Says Thank You to djr33 For This Useful Post:

    jscheuer1 (07-06-2010)

  8. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by djr33 View Post
    "global $var;" anywhere will force a variable to be global.
    Do you mean like this?
    PHP Code:
    <?php

    $myVar 
    'global';
    global 
    $myVar;

    function 
    use_global(){
        echo 
    $myVar;
    }

    use_global();

    ?>
    That was my initial interpretation of the global keyword, but it does not work.
    global actually means "I'm looking for this variable in the global scope".

  9. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    From playing with this a bit, I think:

    "global $var;" anywhere will force a variable to be global.
    Should read:

    global $var; anywhere will force a variable to have its global value in that scope.

    But I may still not have gotten it.

    Thanks for your functions traq, I've tried them and they really show at least some (perhaps all) of what can be done in this regard.

    It appears though that one can't create a variable in a function and have it be available unadorned in the global scope, rather (notwithstanding using the function's return, which is really creating the variable in the global scope in the normal way) it must be created as a property of an existing global object and addressed as such in whatever scope it is later accessed from. Like (not so eloquent as your examples):

    PHP Code:
    $GLOBALS['someVar'] = 'bob';

    function 
    echoBob(){
        echo 
    $GLOBALS['someVar'];
    }

    echoBob(); // outputs bob 
    Last edited by jscheuer1; 07-06-2010 at 01:14 AM. Reason: add a few words
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    actually, Daniel, that reminds me: constants.

    You can make a value available *anywhere* in your script by defining it as a "constant":
    PHP Code:
    <?php

    define
    ('MY_VAR''I am constant as the northern star');

    echo 
    MY_VAR//  works

    function local_scope(){
       echo 
    MY_VAR//  also works
    }

    ?>
    However, constants can only be scalar values (e.g., integer, float, string, boolean - arrays, objects, functions, expressions, etc. don't work).
    PHP Code:
    <?php

    define
    ('MY_VAR''constant');  // scalar; works
    define('MY_VAR2'MY_VAR.' 2');  // two scalars; also works
    define('MY_VAR'1+2);  // expression; doesn't work
    define('MY_VAR'my_function());  // doesn't work, even if the function returns a scalar value

    //  etc.

    ?>

  11. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by jscheuer1 View Post
    global $var; anywhere will force a variable to have its global value in that scope.
    yes.

    Quote Originally Posted by jscheuer1 View Post
    PHP Code:
    $GLOBALS['someVar'] = 'bob'

    function 
    echoBob(){ 
        echo 
    $GLOBALS['someVar']; 


    echoBob(); // outputs bob 
    oh my...

    that gave me an idea.. it works locally (xampp); I'm gonna try it live in a minute. this:
    PHP Code:
    <?php

    function make_global(){
       
    $GLOBALS['someVar'] = 'bob';
    }

    make_global();
    echo 
    $someVar;  //  outputs 'bob'

    ?>
    totally works. exports to the global scope. awesome!
    Last edited by traq; 07-06-2010 at 01:15 AM.

  12. The Following User Says Thank You to traq For This Useful Post:

    jscheuer1 (07-06-2010)

  13. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That looks like the missing link.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •