note 17970 deleted from language.oop by aidan

From: Date: Sat, 03 Jul 2004 14:56:53 +0000
Subject: note 17970 deleted from language.oop by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-72464@lists.php.net to get a copy of this message
Note Submitter: flash@minet.net ---- It's possible to 'redefine' a class using $this. Check this example : <? class A { function A() { print "<div>instance of A</div>"; } function whoami() { print "<div>"; var_dump($this); print "</div>"; } } class B { function B() { print "<div>instance of B</div>"; } function transform() { $this = new A(); // <-- THIS line :) } function whoami() { print "<div>"; var_dump($this); print "</div>"; } } print "<div><b>Instanciations</b></div>"; $A = new A; $A->whoami(); $B = new B; $B->whoami(); print "<div><b>Transformation</b></div>"; $B->transform(); $A->whoami(); $B->whoami(); ?> First, we create two objects : $A -> class A, and $B -> class B. But the method 'transform' redefines $B instances, and $B is now of class A. (sorry, there are some problems with the layout)

« previous php.notes (#72464) next »