Function - PHP accessing global variable set in class

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

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!

Answers

 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 http://c2.com/cgi/wiki?GlobalVariablesAreBad

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

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/31617017/php-accessing-global-variable-set-in-class

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils