- b2evolution CMS Support Forums
- Archives
- Obsolete Forum Threads
- Class making a global reference to itself upon instantiation
1 balupton Jul 10, 2006 05:54
Can anyone help me out here.
I want to make a global variable, and then make it reference to the newly instantiated class.
Heres the code:
<?php
class test {
var $txt = '1';
function test() {
$GLOBALS['test2'] = & $this;
}
function disp($n) {
echo $n.':'.$this->txt.'<br />';
}
}
$test1 = new test();
$test1->disp('test1'); // OUTPUT: test1:1
$test2->disp('test2'); // OUTPUT: test2:1
$test2->txt = '2';
echo '<br />';
$test1->disp('test1'); // OUTPUT: test1:1
$test2->disp('test2'); // OUTPUT: test2:2
// ^ Why aren't they the same :'(
?>
As you can see on the last few lines, the outputs are not the same....
So the reference must of created another copy of the class.... When i want it to refer to the first copy of the class.
Cheers.
EDIT:
As it is a plugin should something like this be used instead?
function AppendPluginRegister ( & $params )
{ // Add a global reference to this class
if( $this->ID > -1 && !isset( $GLOBALS['common_imports_plugin'] ) )
$GLOBALS['common_imports_plugin'] = & $GLOBALS['Plugins']->get_by_ID( $this->ID );
return true;
}