SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Possible to create function that inherits GLOBAL variable scope?

    I am trying to create a php function that does some work, but I need the function to automatically have access to all the variables that are in scope of the calling page.

    Is this possible?

    Basically, I need a "function()" sort of ability.. but it should not have it's own variable scope. It should "use" whatever variables already exist, etc..

    I'm not sure how else to explain what I need..

    Sort of like a function that just does "a group of commands".. but isn't really a true "function()";

    Thanks!

  2. #2
    SitePoint Member
    Join Date
    Nov 2004
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Probably I could grasp what you're looking for. I must say, however, that a function is not able to access variables outside unless they are global or public. Put the variables you need in that function like this:

    PHP Code:
    $GLOBALS['variable'
    And you'll be able to read them inside the function just as well. Alternatively you may list these variables in an array and pass it to the function.

    Japan

  3. #3
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just put your code in a file and include/require when you need it? It will then have access to all the vars in scope at the point it was included.


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
  •