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';
}
1
  • 2
    With regards to the date the questions were asked, I'd actually consider the other question as a duplicate of this one. Commented Apr 9, 2013 at 15:43

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

8 Comments

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.
I suspect the OP knows how to use globals but is looking for an uplevel type of functionality.
@webbiedave, what does that mean? and don't suspect anything, how do you know?
The reason this "works" is because PHP creates variables if they don't already exist. The global statement creates $x in the global scope.
@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: stackoverflow.com/questions/1909647/… and here stackoverflow.com/questions/2067251/…
|
4
class baz {
   private $x;
   public function foo() {
      $this->x = 1;
      return $this->bar();
   }

   public function bar() {
      return $this->x ? 'stuff' : 'other stuff';
   }
}

Comments

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.

Comments

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'; 
  } 

2 Comments

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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.