replacing instances in background

From: Date: Thu, 08 May 2014 09:49:18 +0000
Subject: replacing instances in background
Groups: php.internals 
Request: Send a blank email to internals+get-74051@lists.php.net to get a copy of this message
Hello, What I'm doing is strictly experimental. Please take a look at this code snippet first: class X { public $mark; public function replace_myself_with($php_obj_instance) { ... } } class Y extends X { } $a = new X(); $a->mark = "instanceX1"; $b = new X(); $b->mark = "instanceX2"; $c = $a; $d = new Y(); $d->mark = "instanceY1"; # replace $a with $b, this is the problematic part # an instance of course can't replace itself $a->replace_myself_with($b); # the expected behavior print $a->mark; # replaced, prints "instanceX2" print $b->mark; # unchanged, prints "instanceX2" print $c->mark; # all references are involved, prints "instanceX2" print $d->mark; # unchanged, prints "instanceY1" # now the parent class is different $b->replace_myself_with($d); # the expected behavior print $a->mark; # "instanceY1" print $b->mark; # "instanceY1" print $c->mark; # "instanceY1" print $d->mark; # "instanceY1" As set-up class X has two and class Y has one instance created with 'new'. Variable $a and $c refers to instance marked with "instanceX1" $b refers to "instanceX2" and $d refers to "instanceY1". Is it possible that an instance replaces itself wit another? We might say "replace the instance in the background". Did some research, apparently PHP can't solve this without an extension also many people say this is a bad thing to do. I'm still curious, is it possible? Of course Zend API operates all the time with instances and references and offers many functions for reference manipulation. Using the API replacement of instance1 with instance2, leaving the references which now pointing to instance2 might work. Maybe get all the references referred to instance1 and make them to refer to insance2? Well this is just brainstorming, I don't know how should it work in practice. Is this even possible with a Zend API extension or anyhow? Is there any third-party extension or code example related to this? The guys on ##php IRC channel told me to ask the question on this mailing list. I need a hint to star, any kind of help is welcome. Thank You, Tamás Szárnyasi

« previous php.internals (#74051) next »