}
$reference = new reftest();
$reference->reftest();
$reference->reftest2();
echo $reference->a; echo $reference->c; ?>
However, this doesn't appear to be completely trustworthy. In some cases, it can act strangely.
<?php
class reftest
{
public $a = 1;
public $c = 1;
public function reftest()
{
$b =& $this->a;
$b++;
}
public function reftest2()
{
$d =& $this->c;
$d++;
}
}
$reference = new reftest();
$reference->reftest();
$reference->reftest2();
echo $reference->a; echo $reference->c; ?>
In this second code block, I've changed reftest() so that $b increments instead of just gets changed to 2. Somehow, it winds up equaling 3 instead of 2 as it should.
An interesting if offbeat use for references: Creating an array with an arbitrary number of dimensions.
For example, a function that takes the result set from a database and produces a multidimensional array keyed according to one (or more) columns, which might be useful if you want your result set to be accessible in a hierarchial manner, or even if you just want your results keyed by the values of each row's primary/unique key fields.
<?php
function array_key_by($data, $keys, $dupl = false)
|