In this example, I have an abstract class and two regular classes. The abstract class is not supposed to be used alone, so its constructor is protected. Some functions are defined within the abstract class.
One of those functions is a "clone" function, which is supposed to return a new instance of the current object. This function makes a copy of the current object.
Here's my question:
When trying to set $copy->baz ([2] in clone()), it works, because I'm in the class that defined this private property. However, this doesn't make sense to me (at least in this example), because $copy is another object (of the same class) - is it possible to force PHP to use the magic setter ("Setting private property") when setting a private property of another object (not class)?
abstract class ac
{
private $baz = "fakedefault";
function __set($name, $value)
{
die("Setting private property!");
}
function clone()
{
$copy = clone $this; //make copy
//Test:
$this->baz = "newval"; //[1] Works as expected
$copy->baz = "newval"; //[2] Does not die!
return $copy; //return copy
}
}
class c1 extends ac
{
function foo()
{
print $this->baz;
}
}
class c2 extends ac
{
function foo()
{
print $this->baz;
}
}
function dostuff()
{
$o = new c1();
$o->baz = "thiswontwork"; //Private -> doesn't work
}