views:

116

answers:

4

I'll try to explain with an example...

Let's say I have two different functions, and one of them has a defined variable. In the second function, I don't wanna write the same variable again, can I simply use the variable from the first function in the second one WITHOUT redefining it in the second function?

Something like:

function a()
{
  $var = "my variable";
}

function b()
{
 echo $var;
}

Sorry if this questions is a bit silly, but I'm still a beginner =).

+3  A: 
$a = 1;
function a(){
  global $a;
  $a = 2;
  }
function b(){
  global $a;
  echo $a;
  }

a();
b();

output:

2
fredley
@fredley: The asker is clearly an extreme neophyte if they don't know about global variables yet. Some explanatory text would be instructive.
That is one way to do it, but it gives you global variables. Global variables are the bane of maintainable software, and all programmers, especially PHP programmers, would do well to not use them.
Andy Lester
What if places of the functions are different, let's imagine function b() is first and then after it comes function a(), does this one still apply?
Dugi
+1  A: 

Sure you can do globals, but of PHP 5.3.0+ has anonymous functions, so you can also do closures:

<?php

function a(){
  $a = 1;  
  echo "First: $a, ";
  ++$a;

    // This is a closure. It can gain access to the variables of a() with the 
    // use option.
  $b = function() use ($a) {
      echo "second: $a";  
  };  
  $b();
};
a(); // Outputs: First: 1, second: 2
?>

Try it out at this Codepad example


or probably more useful:

<?php

function a(){
  $a = 1;  
  echo "First: $a, ";
  ++$a;
  $b = function() use ($a) {
      echo "second: $a";  
  };        
  return $b;
};
$fun = a();     // $fun is now $b & $b has access to $a!
$fun();
// Outputs: First: 1, second: 2
?>

Try it out at this Codepad example

From the docs:

Closures may also inherit variables from the parent scope. Any such variables must be declared in the function header. Inheriting variables from the parent scope is not the same as using global variables. Global variables exist in the global scope, which is the same no matter what function is executing. The parent scope of a closure is the function in which the closure was declared (not necessarily the function it was called from).

Peter Ajtai
Interesting typo consistency ("fist" instead of "first", both times).
@user359996 - Thanks. Copy paste will do that ;)
Peter Ajtai
+6  A: 

The cleanest solution here is to make A and B methods of a class and have the common variables as private static variables. This removes the messiness of globals and allows both functions to be globally accessible.

class SomeClass
{
    private static $a = 1;

    public static function a() {
        self::$a = 2;
    }

    public static function b() {
        echo self::$a;
    }
}

You can then call the functions:

SomeClass::a();
SomeClass::b();
lonesomeday
+1  A: 

I'd do it like this. This is a more common implementation. Remember that functions can be passed parameters and can return values.

// Main
$a = a();
b($a);

function a()
{
  return "my variable";
}

function b(a$)
{
 echo $a;
}

You have the main body of a program, and the variable a$ remains in scope there. Globals are rarely used, and technically never needed.

Marcus Adams