views:

166

answers:

3

Hi guys, please tell me theres a way around this...

$my_var = 'hello';

class Test{

    private $my_var;
    private $my_internal_var = 'if you see this it works!';

    function __construct(){
        global $my_var;

        $this->my_var = &$my_var;
    }

    function get_my_var(){
        return $this->my_var;
    }

    function set_my_var($value){
        $this->my_var = $value;
    }

    function set_my_var_to_internal_reference(){
        //this line should make $my_var in root, $my_var in this object, and $my_var in
        //all external functions point to $my_internal_var.
        $this->my_var = &$this->my_internal_var;
    }
}


function get_my_var(){
    global $my_var;

    return $my_var;
}

function set_my_var($value){
    global $my_var;

    $my_var = $value;
}


$my_obj = new Test();

echo '<h2>set default starting value</h2>';
echo 'obj : '.$my_obj->get_my_var().'<br>'; //echoes 'hello' - this is ok
echo 'func: '.get_my_var().'<br>'; //echoes 'hello' - this is ok
echo 'root: '.$my_var.'<br>'; //echoes 'hello' - this is ok

set_my_var('hello world!');

echo '<h2>set to value by function</h2>';
echo 'obj : '.$my_obj->get_my_var().'<br>'; //echoes 'hello world!' - this is ok
echo 'func: '.get_my_var().'<br>'; //echoes 'hello world!' - this is ok
echo 'root: '.$my_var.'<br>'; //echoes 'hello world!' - this is ok

$my_obj->set_my_var('hello world again!');

echo '<h2>set to value by object method</h2>';
echo 'obj : '.$my_obj->get_my_var().'<br>'; //echoes 'hello world again!' - this is ok
echo 'func: '.get_my_var().'<br>'; //echoes 'hello world again!' - this is ok
echo 'root: '.$my_var.'<br>'; //echoes 'hello world again!' - this is ok

$my_obj->set_my_var_to_internal_reference();

echo '<h2>set to object internal reference</h2>';
echo 'obj : '.$my_obj->get_my_var().'<br>'; //echoes '      ' - this is NOT ok. should be 'if you see this it works!'
echo 'func: '.get_my_var().'<br>'; //echoes 'hello world again!' - this is NOT ok. should be 'if you see this it works!'
echo 'root: '.$my_var.'<br>'; //echoes 'hello world again!' - this is NOT ok. should be 'if you see this it works!'

Thanks!!

A: 

Use an ampersand (&) in front of a variable in the function arguments definition to force it to pass by reference. Likewise, use an ampersand in front of the value which you are assigning to a variable to force an assignment by reference instead of by value.

function foo(&$var)
{
    // $var is passed by reference to this function
}

$var = $var2;  // assigns $var the value of $var2
$var = &$var2; // assigns $var the same reference as $var2

For more detail, see the PHP manual section References Explained.

Amber
+1  A: 

This is NOT ok as is. You should use a wrapper object in OO enviorment, and don't use global vars, they are sucks. Example:

class My_Registry{
    private $registry = array();

    public function set_var($key, $var){
        $this->registry[$key] = $var;
        return $this;
    }

    public function get_var($key){
       if(isset($this->registry[$key]){
           return $this->registry[$key]
       }else{
           return false;
       }
    }
}

Or something like this, you can add error handling, privileges, etc.

erenon
A: 

$my_internal_var is a global and not a class variable, so this line can't possibly work:

$this->my_var = &$this->my_internal_var;

$this->my_internal_var doesn't exist.

Why aren't you using the same logic you're already using in some of your other functions/methods? For setting one global variable to a reference of another, though, you need to use the $GLOBALS array:

function set_my_var_to_internal_reference(){
    global $my_internal_var;

    $this->my_var = &$my_internal_var;
    $GLOBALS['my_var'] = &$my_internal_var;
}

Have a look at the PHP manual page about variable scope.

Oh, and as much as this may make it work for you now, I'm with erenon in that you should avoid using globals...

mercator