2

I'm missing something fundamental here. We need to access the variable $myvar that is set in the function do_stuff. Here's a generalized snippet of the class where the function exists. The class and it's functions all work just fine.

class TestClass {

    public function do_stuff() {
        global $myvar;      
        $myvar= 'the value we want';
    }
}

Now, later on here's where it goes wrong...

$TestClass = new TestClass();
$TestClass->do_stuff();

function get_var() {
    global $myvar;
    echo $myvar; // here the value does NOT exist
}
get_var();

echo $myvar; // but here the value DOES exist

So what am I missing as to why the variable exists when we access it outside of a function, but it does not exist when accessing inside a function?

Notes: This is a simplified version of what we're doing, so although this example is trivial, it illustrates the issue we're facing. I also understand that many people don't like global variables. Given the legacy code and framework being used, we need to solve this specific problem.

Thank you for you help!

4
  • 3
    Could you update this simplified version of your code wiith the part that would set a value to $myvar? I don't see TestClass::do_stuff being called in this example. Commented Jul 24, 2015 at 18:09
  • What order are you doing these things in, where are you calling the class from, when are you including it for example. If you try to get the value before you set it then of course it will be null Commented Jul 24, 2015 at 18:17
  • @ArtisiticPhoenix I updated the code to indicate how the class is used. I had omitted that previously for simplicity, but I obviously it wasn't clear enough. Commented Jul 24, 2015 at 19:35
  • @MattSmith - it works fine for me doing that see update, this is a function as you have it, not a closure, right? Commented Jul 24, 2015 at 22:51

2 Answers 2

2
 class TestClass {

    public function do_stuff() {
        global $myvar;      
        $myvar= 'the value we want';
    }
}

function get_var() {
    global $myvar;
    echo $myvar; // here the value does NOT exist
}
get_var();

echo $myvar; // but here the value DOES exist

Will be what you got, now if you actual use the class and execute it.

 class TestClass {

    public function do_stuff() {
        global $myvar;      
        $myvar= 'the value we want';
    }
}


function get_var() {
    global $myvar;
    echo $myvar; // here the value does NOT exist
}

$TestClass = new TestClass();
$TestClass->do_stuff();
get_var();

echo $myvar; 

Should echo what you want. Order of operations still matter, just because something is placed in a class doesn't mean it get's magically executed.
For example this

   get_var();
   $TestClass = new TestClass();
   $TestClass->do_stuff();

Is still unset

To be completely honest though, I would avoid using global at all because the main issue with that method is that you cannot trace where the value in it is from, For example using a class to hold the value, it is then very easy to find the class. To me use of code like that is just lazyness on the part of the developer.

UPDATE: that is odd then because running this:

class TestClass {

    public function do_stuff() {
        global $myvar;
        $myvar= 'the value we want';
    }
}


$TestClass = new TestClass();
$TestClass->do_stuff();

function get_var() {
    global $myvar;
    echo $myvar; // here the value does NOT exist
}
get_var();

echo $myvar;

ON WAMP with php 5.4 works fine and outputs

the value we wantthe value we want

Note though I ran it as a complete block, in the same file. But again I feel I have to say that you would be better off injecting the value into the function instead of mucking with global it's something that should be done away with IMO.

$TestClass = new TestClass();


get_var($TestClass->do_stuff());

What's the purpose of get_var, I know this is an example. But what are you trying to do that you need global for. There are other things that can be done such as throw and catch exceptions, debug_backtrace(), dependancy injection being the obvious one.

here is just one of may articles you can find on the subject.

http://c2.com/cgi/wiki?GlobalVariablesAreBad

As a professional web developer and a CIO, I have to agree with them.

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

1 Comment

I updated the code to indicate how the class is used. I had omitted that previously for simplicity, but I obviously it wasn't clear enough. Thanks for helping!
-1

The correct would declare the variable outside the function and then use global, or this to call the variable.

$myVar = null;

funcion get_var(){
  global $myVar;
  $myVar = 10;

  for ($i = 0; i < $myVar; $i++){
    var_dump($i);
  }
}

Works fine for me.

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.