Is it possible to override a method for a single instance? e.g.
class test
{
protected function print()
{
echo "printed from old method";
}
}
$a= new test();
can i overrride method "print" only for object $a?
e.g. $a->test=function(){echo "printed with new method";}
I know its abit weird but in my project a common class ("base") is part of many other classes each of which needs a slightly modified version of "base". I just need to override one method each time. Trying to avoid creating a huge number of subclasses of "base" for this purpose.
p.s. i have seen the solution you propose but it doesn't work for me. php version is 7.4.3
here is my code:
class nodeGroup
{
public function test()
{
echo "internal";
}
}
$a= new nodeGroup();
$a->test = (function() {
echo "external";
})->bindTo($a);
$a->test();
result is "internal"