assignment to $this inside constructor

From: Date: Wed, 04 Dec 2002 13:15:46 +0000
Subject: assignment to $this inside constructor
Groups: php.general 
Request: Send a blank email to php-general+get-126977@lists.php.net to get a copy of this message
Let's begin with a small code snippet: <code> class A { function A( $selector ) { switch( $selector ) { case 1: $obj = new A_1(); break; case 2: $obj = new A_2(); break; ... } $this = $obj; } } class A_1 { ... } class A_2 { ... } $a = new A(1); // $a is now an instance of A_1 $a = new A(2); // $a is now an instance of A_2 </code> I use this trick to implement "polymorphic" objects. I just wanted to know what other people think about it. Is this a "dangerous" technique? (i.e. changes in php object model can break programs that use it) Is it already in common use? Any opinion is welcome. Thanks fbv

« previous php.general (#126977) next »