Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    405
    Thanks
    17
    Thanked 2 Times in 2 Posts

    how to get external variable into function scope.

    I know.. I know. an external variable can't be passed into a function. It's out of scope. But I thought there was some workaround for this (besides GLOBAL - don't want that)

    I know I can declare the variable right at the start, but I'm trying to separate things out.

    I know this will work...
    PHP Code:
    function doSomething($var1$var2 "variable-I-wanna-pass")
    {
     
    // do something

    So I was hoping this would work (or something like it):

    PHP Code:
    $var2 "variable-I-wanna-pass";

    function 
    doSomething($var1$var2)
    {
     
    // do something

    but it didn't.

    EDIT: Clarification. Part of the problem is that I am NOT at liberty to change the function to something else. But I can add in my stuff.
    Last edited by turpentyne; 03-14-2014 at 04:36 PM.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by turpentyne View Post
    So I was hoping this would work (or something like it):

    PHP Code:
    $var2 "variable-I-wanna-pass";

    function 
    doSomething($var1$var2)
    {
     
    // do something

    but it didn't.
    Excuse the really stupid question but did you call the function? Only reason I ask is that you don't show a function call. This is what I mean:

    PHP Code:
    $var2 "variable-I-wanna-pass";

    doSomething($var1$var2); //THIS BIT - Did you try this?

    function doSomething($var1$var2)
    {
     
    // do something

    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    What you can't change is likely referring to the function signature. In PHP, this is actually only bound on two scenarios: the first being by-reference and the second being by contract (ie: extended class method or interface). Otherwise, you never need to declare your parameter list in PHP and can use func_get_arg[s] functions to pull the arguments provided.

    Your question is unclear. The examples you are showing is making use of PHP's optional parameter passing (due to not having overload capabilities), but you appear to be asking for the scope argument which is what Tango is asking about.

    So putting the two together, I believe you're asking if you can specify a variable as the optional parameter value without providing a globalized call, and the answer is no you cannot. In object oriented world this is never a problem as you can pass an object through.
    You can however pass a string representation of any type and interpret it at runtime. The easiest would be either a function call or a constant:
    PHP Code:
    function aFunc($p 'CONSTANT')
    {
        print 
    constant($p);
    }
    define('CONSTANT''A Value');
    aFunc();

    function 
    bFunc($p 'AFUNC')
    {
        print 
    $p();
    }
    bFunc(); 
    Both however have little/no value since a defined constant is global in any scope, and functions global to the scope defined (typically global, or classes and namespaces in objects). So it is a waste to call constant($p) when I can just call CONSTANT directly. The plus I guess is you can tell it *which* constant or function to call.

    OOP doesn't have this problem since member properties can be stored within a class itself. You never need to provide to a class method if you instead provide to the object itself. There are several ways to read and share data between scopes both with and without objects.

    In procedural, simply write a function that's job is to give out information for requested configurations. All it has to do is check to see if a static variable is null, and if so, parse a file/db for the data it needs. Store the information and hand it back to the requester. Then you can use that in your functions:
    PHP Code:
    function getSomething($on null)
    {
        
    $on $on == null getOn('whatIDefaultTo') : $on;

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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