note 17970 added to language.oop
| From: | flash at minet dot net | Date: | Sat, 05 Jan 2002 16:21:44 +0000 |
| Subject: | note 17970 added to language.oop | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-24428@lists.php.net to get a copy of this message | ||
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)
--
http://www.php.net/manual/en/language.oop.php
http://master.php.net/manage/user-notes.php?action=edit+17970
http://master.php.net/manage/user-notes.php?action=delete+17970
http://master.php.net/manage/user-notes.php?action=reject+17970