Re: assignment to $this inside constructor
| From: | Bastian Vogt | Date: | Wed, 04 Dec 2002 14:09:03 +0000 |
| Subject: | Re: assignment to $this inside constructor | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-126979@lists.php.net to get a copy of this message | ||
Hi,
I suggest reading from page 12 of this document:
http://www.zend.com/engine2/ZendEngine-2.0.pdf
There's an example which matches with your technique....
Regards,
Bastian
Filippo Veneri schrieb:
> 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