I\'d like to reuse a functionality several times in a single class. This functionality relies on a private variable:
tr
Some years down the line, I followed up on a comment in bug 63629 and produced the following:
addresses[substr($calledAs, 3)];
}
public function setAddress($address) {
$calledAs = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]['function'];
$this->addresses[substr($calledAs, 3)] = $address;
}
}
class TestClass
{
use TestTrait { getAddress as getHomeAddress; setAddress as setHomeAddress; }
use TestTrait { getAddress as getWorkAddress; setAddress as setWorkAddress; }
}
$c = new TestClass();
$c->setHomeAddress("High Street, Luton");
echo $c->getHomeAddress();
echo "\n";
$c->setWorkAddress("Business Name, London");
echo $c->getWorkAddress();
echo "\n";
which outputs
High Street, Luton
Business Name, London
It can be done! (With thanks to Dave Farrell whose answer inspired this one.) The arguments to debug_backtrace are an attempt to minimize memory usage, I'm not certain how much of an effect on performance that is.