<?php
/* Example for phpGlobalRef
*/
include_once("phpglobalref.php");
class class1{
var $Ref;
var $Class2_ref;
function class1()
{
$this->Ref = new phpGlobalRef;
}
function getclass2()
{
$this->Class2_ref = &$this->Ref->getRef("class2");
}
function class2counter()
{
$this->Class2_ref->classcounter();
}
}
class class2{
var $Ref;
var $Counter;
function class2()
{
$this->Counter = 0;
}
function classcounter()
{
$this->Counter++;
echo $this->Counter;
}
}
// Create Class1 ,Class2 and phpGlobalRef
$Class1 = new class1;
$Class2 = new class2;
$Ref = new phpGlobalRef;
// Add class2 to Globalref (!MUST be added from outside the class to get a real reference!)
$Ref->addRef($Class2, "class2");
// Add reference of class2 to class1
$Class1->getclass2(); // Or $Class1->Class2_ref = &$Ref->getRef("class2");
// Run class2::classname with the class1 object
$Class1->Class2_ref->classcounter();
// call class2::classname to increment counter and see if Class2_ref is a reference of class2
$Class2->classcounter();
$Class2->classcounter();
// Run class2::classname with the class1 object again
$Class1->class2counter();
/* Output must be:
1234 if ok or
1122 if something went wrong
*/
?>
|