Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: PHP Global Object

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

    Default

    Correct in your replies: global $var; tells the function to collapse the local and global values into the global variable.
    There is no way to create a "superglobal" as in $_GET that will then be ALWAYS global. But using global or $GLOBALS[] will allow you to access them as needed.

    Unless you're doing a lot with the value, my favorite method is this:
    PHP Code:
    function myfunc() {
       
    $myvar $GLOBALS['myvar'];
       echo 
    $myvar//or something else...


    Globals can get complicated, but the simple rule I use is this: if you need to use a global value, declare global $var; as the first line inside your function (or possibly within a conditional if needed). Alternatively use $GLOBALS and there will be no confusion (hopefully), and at the very least no ambiguity.

    What I don't know about the 'global' keyword is what happens when you do this at a weird time:
    PHP Code:
    $myvar 1;
    function 
    myfunc() {
    $myvar 2;
    global 
    $myvar;
    echo 
    $myvar//2? 1?
    }
    myfunc();
    echo 
    $myvar//2? 1? 
    I haven't tested this (never really needed to), but this is a possible place of much confusion so just make sure you set everything global you need at the start or use $GLOBALS, and you'll be fine.

    I'm planning to rewrite everything on my current project, when I get to it, to use $GLOBALS instead of 'global'.


    Now, if you want to play with something more confusing, the & operator in PHP is very complex. This is used in function calls.
    PHP Code:
    $var 1;
    function 
    func(&$v) {
    $v 2;
    }
    func($var);
    echo 
    $var//2!! 
    And it can do other things. Declaring "&" at runtime generates a deprecated warning, though. (Ex: calling myfunc(&$var);, rather than having that as a default in the function definition).


    traq, regarding constants:
    I hate constants. I wish they were redefinable. I guess this would change their nature entirely, but they're very near useless except to store never-variable information. And at that, it's much more likely that you'll not even care what's in them, but that they are set at all.
    The only exception I make to this is that they're great for a security system: on your primary page (index?) establish:
    define('MYSITE',1);
    Then in any other pages, to make sure this is being accessed properly:
    if (!is_defined('MYSITE')) { exit; }
    And that's it. This allows you to ignore scope and just deal with this simple value. You could also do more things like set that the user has passed the login, etc., but that gets too variable (no pun intended), and constants are no longer very helpful.
    Last edited by djr33; 07-06-2010 at 01:34 AM.
    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

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

    traq (07-06-2010)

  3. #12
    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
    What I don't know about the 'global' keyword is what happens when you do this at a weird time:
    PHP Code:
    $myvar 1;
    function 
    myfunc() {
    $myvar 2;
    global 
    $myvar;
    echo 
    $myvar//2? 1?
    }
    myfunc();
    echo 
    $myvar//2? 1? 
    I got '1' both times. not what I expected, though.
    Quote Originally Posted by djr33 View Post
    Now, if you want to play with something more confusing, the & operator in PHP is very complex. This is used in function calls.
    PHP Code:
    $var 1;
    function 
    func(&$v) {
    $v 2;
    }
    func($var);
    echo 
    $var//2!! 
    geez, I hate that stupid thing. however, your example seems to allow you to set a global variable from a local scope, so that's good to know.

    Quote Originally Posted by djr33 View Post
    traq, regarding constants:
    ...
    they're great for a security system: on your primary page (index?) establish:
    define('MYSITE',1);
    Then in any other pages, to make sure this is being accessed properly:
    if (!is_defined('MYSITE')) { exit; }
    absolutely. I love that. I take it a step further:
    PHP Code:
    <?php

    if (!is_defined('MYSITE')) { header("Location:  http://www.mysite.com/index.php"); exit; }

    ?>
    One thing I do find constants useful for is defining site-wide chunks of "whatever" that may need to be used in several, or varying, places. Such as:
    PHP Code:
    <?php

    define
    ('DESIGNER''Web design by <a href="http://www.custom-anything.com/">Adrian</a>');

    //  blahblahblah...

    $footer 'Site &copy; 2010 by a Client. &nbsp;'.DESIGNER;
    echo 
    $footer;

    ?>

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

    Default

    Constants are fine but not flexible. If you have some reason these might vary or do something odd in any case, they won't work. In general, probably one third of our variables could be replaced by constants, but without any real reason except that we don't change the values.
    However, if you're using that sort of code in a few places it's not a bad idea. It does get around the scope easily.
    And I agree that for security the method is good, but I've found constants otherwise frustratingly limited. A header is fine, though where I'm using this I hope no one tries to go there, since it's never meant to be public (or even viewed by me). Of course for a login or something it would be different.


    Interesting about "1" being the return value. I'd think the exact opposite. But this means then that "global $var;" is basically just an alias for "$var = &$GLOBALS['var'];"

    Note: the & in this case apparently links the two variables to act as one: one is the alias of the other, tied together unless you do something else. Unsetting the alias just removes that, not the variable, though.

    In fact, it's practically this function:
    PHP Code:
    function global(&$var) {
    $var = &$GLOBALS[$var];

    And there's the annoying "&". In this case there's no other way to access that local variable.
    Note that & allows us not only globals but also higher local variables... interesting.
    (I can't ever remember a time I have used &, except when modifying someone else's script...)

    That just reached the extent of my knowledge about &, and I'm not even sure it's used correctly there, but that's close enough for an example I guess.
    Last edited by djr33; 07-06-2010 at 03:05 AM.
    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

  5. #14
    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
    Interesting about "1" being the return value. I'd think the exact opposite. But this means then that "global $var;" is basically just an alias for "$var = &$GLOBALS['var'];"
    yeah.

    my understanding of global is that it does the same thing as $GLOBALS, but is persistent (you can use $var directly instead of typing out $GLOBALS['var'] every time).

    I read (somewhere) about the & creating a "reference" to the content of another variable - allowing both variables to point at (and therefore, work with) the same content. Something like having several shortcuts pointing at the same file (or, probably more like several filenames pointing at the same data). Each reference has full access to the content. Of course, I wonder what would happen if:
    Edit: corrected experiment code
    PHP Code:
    <?php

    $myVar 
    1//  global

    function local(&$v){
       
    $v 2// local
       
    more_local($v);
       echo 
    'local: '.$v;
    }

    function 
    more_local(&$v){
       
    $v $v+1;
       echo 
    'more local: '.$v;
    }

    local($myVar);
    echo 
    'global: '.$myVar;

    ?>
    any predictions?
    Last edited by traq; 07-06-2010 at 03:57 AM.

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

    Default

    It should change it's value each time and finally be global with the new value.
    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. #16
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    yeah, all three echoes take place after the more_local function does its modification, so the output is "3" each time. That experiment wasn't as confusing/interesting as I thought it would be... oh well.

  8. #17
    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

    I Still liked it. 3 3 3

    Anyways, though in itself virtually useless, I think this is interesting:

    PHP Code:
    <?php
        $someVar 
    'bob';
        echo 
    $GLOBALS['someVar'] . '<br>'// outputs bob<br>
        
        
    function changeVar(&$var$val){
            
    $var $val;
        }
        
        
    changeVar($someVar'ted');
        
        echo 
    $someVar// outputs ted
    ?>
    - John
    ________________________

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

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

  10. #19
    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

    Nerds are cool now, didja know?
    - 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
  •