如何通过引用将类var传递给PHP中的全局变量?

时间:2009-09-02 21:45:15

标签: php pass-by-reference

请告诉我这是一个方法......

$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!'

谢谢!

3 个答案:

答案 0 :(得分:2)

这不是正常的。您应该在OO环境中使用包装器对象,并且不使用全局变量,它们很糟糕。 示例

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

或类似的东西,你可以添加错误处理,权限等。

答案 1 :(得分:0)

在函数参数定义中的变量前面使用&符号(&)来强制它通过引用传递。同样,在分配给变量的值前面使用&符号来强制通过引用而不是按值强制赋值。

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

有关更多详细信息,请参阅PHP手册部分References Explained

答案 2 :(得分:0)

$my_internal_var是一个全局变量,而不是一个类变量,所以这行不可行:

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

$this->my_internal_var不存在。

为什么你没有使用你已经在其他一些功能/方法中使用的相同逻辑?但是,要将一个全局变量设置为另一个全局变量,需要使用$GLOBALS数组:

function set_my_var_to_internal_reference(){
    global $my_internal_var;

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

查看PHP manual page about variable scope

哦,尽管这可能会让它现在对你起作用,但我还是erenon,你应该避免使用全局...