2

I know this is kind of a hacky thing I'm trying here, but I'm wondering if this is even possible. is there anyway I can access the $x variable in the following code, without passing arguments?

function foo() {
 $x = 1;
 return bar();
}

function bar() {
  //get $x from foo here somehow?
  return $x ? 'stuff' : 'other stuff';
}
GSto
  • 41,512
  • 37
  • 133
  • 184
  • 2
    With regards to the date the questions were asked, I'd actually consider the *other* question as a duplicate of this one. – RandomSeed Apr 09 '13 at 15:43

4 Answers4

4

I am not sure why, but you can use globals that opens up to a whole thing, but i will show you:

function foo() {
 global $x;
 $x = 1;
 return bar();
}

function bar() {
  global $x;
  //get $x from foo here somehow?
  return $x ? 'stuff' : 'other stuff';
}

Here is the demo: http://codepad.org/fPqUXzyC

It is always better to not use globals and just pass parameters, but if you cannot you could use globals

Naftali
  • 144,921
  • 39
  • 244
  • 303
  • I agree, the code ends up becoming a lot less manageable and more prone to lots of dependency issues if you are using variables in the global scope without ... scoping them somehow within objects or something similar. – Anther May 10 '11 at 19:42
  • I suspect the OP knows how to use globals but is looking for an `uplevel` type of functionality. – webbiedave May 10 '11 at 19:44
  • @webbiedave, what does that mean? and don't `suspect` anything, how do you know? – Naftali May 10 '11 at 19:45
  • The reason this "works" is because PHP creates variables if they don't already exist. The `global` statement creates `$x` in the global scope. – webbiedave May 10 '11 at 19:46
  • @Neal: I don't know. I only suspect it because he is already a programmer in multiple languages, takes computer science classes and stated he was trying to do something "hacky". Also, check out his answer here: http://stackoverflow.com/questions/1909647/make-all-variables-global-php/1909663#1909663 and here http://stackoverflow.com/questions/2067251/what-is-the-best-way-to-pass-or-access-other-objects-in-other-classes-in-php/2067266#2067266 – webbiedave May 10 '11 at 19:51
  • @webbiedave, i have no idea :-p – Naftali May 10 '11 at 19:52
  • @Neal: I borrowed `uplevel` from TCL which allows access to variables up the stack. PHP has no equivalent capability. – webbiedave May 10 '11 at 19:56
  • you might update you answer .. suggesting people to use `global` is a really bad thing – tereško Jun 11 '12 at 10:24
4
class baz {
   private $x;
   public function foo() {
      $this->x = 1;
      return $this->bar();
   }

   public function bar() {
      return $this->x ? 'stuff' : 'other stuff';
   }
}
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
3

You could make foo() store the $x value to $GLOBALS[] or global $x;. Other than that, nothing I can think of would do it. It would need to be purposely exposed to get it from inside another function.

If this is your code, I may recommend thinking about taking an object-oriented approach.

class Foo
{
  public static $x;

  public static function Foo(){
    Foo::$x = 1;
    return Foo::Bar();
  }

  public static function Bar() {
    return Foo::$x ? 'stuff' : 'other stuff';
  }
}

echo Foo::Foo();

Alternatively, do as others have suggested and pass $x as a function parameter.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
0

I know this is kind of old, and an answer has already been accepted, but wouldn't this work?

function foo() 
  {  
    $x = 1;  
    return bar($x); 
  }  

function bar($x) 
  {
    return $x ? 'stuff' : 'other stuff'; 
  } 
TecBrat
  • 3,643
  • 3
  • 28
  • 45
  • as I said in the question, it had to be without accepting agruments, as I couldn't change the signature of the `bar()` function. so no, this would not work in this scenario. – GSto Feb 15 '12 at 19:43
  • Sorry, I overlooked that part. Do I understand correctly that you cannot edit bar() at all? If you could, then you might use func_get_args to allow you to pass in another value. – TecBrat Feb 17 '12 at 03:06