3

suppose i have two nested function like this :

$a = 1;
$b = 2;

function test(){
    $b  =   20;
    function Sum()
    {   
        $b  =   $GLOBALS['a']   +   $b;
    }
}
test();
Sum();
echo $b;

now i want in function Sum() access to $b variable declared in function test();
How do you do?

4
  • 1
    It is not right to nest functions like this in PHP. What you want to do has become possible in PHP 5.3 but I'm not sure it's really what you need Commented Nov 7, 2011 at 11:33
  • this example do not certain task. this is just an example for that how to get $b declared in test() function in sum() function. Commented Nov 7, 2011 at 11:38
  • Then please provide an example/description that shows more of the actual use-case. Nesting function definitions like in the current example does nothing to the variable visibility, function Sum is declared and defined in the global scope just like function test is, the definition is only "postponed" until function test is executed. With php 5.3 (as Pekka has mentioned) lambda functions/closures have been introduced. If they are what you're looking for depends on what you're actually trying to achieve. Commented Nov 7, 2011 at 11:58
  • Use return-statements instead of globals. $b = sum($a, 20); could work just fine. Commented Nov 7, 2011 at 12:09

3 Answers 3

5

Wild-Guessing-mode:
Your function Sum() would "normaly" take two parameters/operands like

function Sum($a, $b) {  
  return $a+$b;
}
echo Sum(1, 20);

Now you have the function Test() and you want it to return a function fn that takes only one parameter and then calls Sum($a, $b) with one "pre-defined" parameter and the one passed to fn.

That's called either currying or partial application (depending on what exactly you implement) and you can do something like that with lambda functions/closures since php 5.3

<?php
function Sum($a, $b) {
    return $a + $b;
}

function foo($a) {
    return function($b) use ($a) {
        return Sum($a, $b);
    };
}

$fn = foo(1) // -> Sum(1, $b);
$fn = foo(2) // -> Sum(2, $b);
echo $fn(47);
Sign up to request clarification or add additional context in comments.

Comments

1

Why not use this?

$a = 1;
$b = 2;

function test(){
    $b = Sum(20);
}

function Sum($value)
{   
    $value = $GLOBALS['a'] + $value;
    return $value;
}

test();
// Sum(); // Why do you need this here??
echo $b;

Edit: Better without globals

$a = 1;
$b = 2;

function Sum($value1, $value2)
{   
    return $value1 + $value2;
}

$b = 20; // you could call Sum($a, 20); instead
$b = Sum($a, $b);
echo $b;

Comments

1

According to the Context best way is to pass the variable to the function like this

Sum($b)

But if you are looking for an alternative then you can use closures but REMEMBER PHP<5.3 does not support closures

You can do

$a = 1;
$b = 2;

function test() {
   $b  =   20;
   function Sum() use($b)
   {   
      $b  =   $GLOBALS['a']   +   $b;
  }
}
test();
Sum();
echo $b;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.