請告訴我那裏有解決的辦法......如何通過引用類var到PHP中的全局變量?
$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!'
謝謝!
感謝erenon,事情是我已經嘗試過註冊表模式,單身人士等,這是什麼給了我最成功的結果。我需要的是一種方式來進行更改或讀取一個隨處可用的var,然後通過var到對象中的var。所以我的想法是讓「無處不在」變量成爲對象內部變量的引用。但是這沒有發生。我知道我設置的引用工作正常,因爲它在幾乎所有情況下都能正常工作,除非當我嘗試將引用var =引用到另一個引用時。 $ everywhere_var -references-> $ obj_var1 -refs-> $ obj_var2 – Juank 2009-09-02 23:08:31