Results 1 to 7 of 7

Thread: [RESOLVED] php5 - global variable clarification

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Godzone, oops Oz
    Posts
    355

    Resolved [RESOLVED] php5 - global variable clarification

    Code:
     
    
    $a = 1;
    $b = 2;
    
    
    function nekkidrage()
    
       global $a, $b;
    
       exit()
    Will $a = 1 and $b = 2 in the function? If so is this the best approach?

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: php5 - global variable clarification

    yes, they will be available to the function. this approach is perfectly fine. you could also use the $GLOBALS array, but I prefer the method you posted.

    PHP Code:
    function sum(){
      return 
    $GLOBALS['a'] + $GLOBALS['b'];

    more on variable scope

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: php5 - global variable clarification

    I prefer not to use globals — for things which require global access, I use static variables in classes.

    This approach gives you organisation: you can have a settings class, a database class, and so on.

    A function to me should be self-contained (unlike a class method). It should be stateless and deterministic — the same input should always give the same output — unless it is specifically otherwise (such as a pseudo-random algorithm).
    Last edited by penagate; Apr 16th, 2010 at 12:16 AM.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Godzone, oops Oz
    Posts
    355

    Re: php5 - global variable clarification

    Quote Originally Posted by penagate View Post
    I prefer not to use globals — for things which require global access, I use static variables in classes.

    This approach gives you organisation: you can have a settings class, a database class, and so on.

    A function to me should be self-contained (unlike a class method). This means it takes input (arguments) and returns output, which doesn't depend on any other factors.
    Baby steps for me dude, learning php as I go. Maybe next verison of the software

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: php5 - global variable clarification

    That's general advice, not just for PHP. Your approach is perfectly valid; I'm just offering another viewpoint.

    Objects exist to provide state and so my opinion is that any function which requires state should be a class method.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Godzone, oops Oz
    Posts
    355

    Re: php5 - global variable clarification

    Quote Originally Posted by penagate View Post
    That's general advice, not just for PHP. Your approach is perfectly valid; I'm just offering another viewpoint.

    Objects exist to provide state and so my opinion is that any function which requires state should be a class method.
    Not dissing the approach, simply stating there's a time factor involved so as much as I might like to add all the decent programming approaches I don't have time to learn them all.

  7. #7
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: php5 - global variable clarification

    Nice function name, by the way...

Posting Permissions

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



Click Here to Expand Forum to Full Width