Fwd: Re: [php-objects] assign to $this in constructor.
| From: | Jesus M. Castagnetto | Date: | Wed, 01 Jan 2003 21:55:25 +0000 |
| Subject: | Fwd: Re: [php-objects] assign to $this in constructor. | ||
| Groups: | php.pear.dev php.pear.general | ||
| Request: | Send a blank email to pear-dev+get-11989@lists.php.net to get a copy of this message | ||
Forwading this msg to pear-dev and pear-general too.
--- "Jesus M. Castagnetto" <jcastagnetto@yahoo.com> wrote:
> Date: Wed, 1 Jan 2003 13:44:58 -0800 (PST)
> From: "Jesus M. Castagnetto" <jcastagnetto@yahoo.com>
> Subject: Re: [php-objects] assign to $this in constructor.
> To: php-objects@yahoogroups.com
>
>
> --- Matt Friedman <matt@sprynewmedia.com> wrote:
> > I think I was once able to assign to the $this reference in the
> > constructor of a php class so that the new operator would return the
> > object I've assigned rather than the object requested. I don't seem to
> > be able to do this anymore. Is this new in 4.3?
>
> The previous behaviour sounds to me like the bug ;-). Usually when I need
to
> handle this dynamic creation of objects, I use a factory method from a
> manager
> class. It could be from an instance of the manager class or from a static
> method from a class.
>
> The code below has been tested on 3 versions of PHP (4.1.2, 4.3.0 and
> 4.4.0-dev
> from CVS), none returns an "Error" object, but a "Foo" object.
>
>
> class Error {
> var $msg;
> function Error($msg) {
> $this->msg = $msg;
> }
> }
>
> class Foo {
> var $n;
> function &Foo($n) {
> if ($n < 0) {
> $this =& new Error('Foo does not like that negative stuff');
> } else {
> $this->n = $n;
> }
> }
> }
>
> $a = new Foo(-1);
> print_r($a);
>
> The ouput is as follows:
>
> 4.1.2:
>
> foo Object
> (
> )
>
> 4.3.0 and 4.4.0-dev from CVS:
>
> foo Object
> (
> [n] =>
> )
>
>
> > Is there anyway to alter what object is returned from a php class
> > constructor?
>
> Nope. BTW, do you know of an OOP pattern, construct or language that allows
> you
> do that. I would like to learn.
>
> > For instance, there might have been an error so you would return an
> > error object instead of the requested object.
>
> Why not use a class organization like the one below.
>
> class Error {
> var $msg;
> function Error($msg) {
> $this->msg = $msg;
> }
> }
>
> class Foo {
> var $n;
> function &Foo($n) {
> $this->n = $n;
> }
> }
>
> class Factory {
> function &createFoo($n) {
> if ($n < 0) {
> return new Error('Foo does not like negatives');
> } else {
> return new Foo($n);
> }
> }
> }
>
> $a =& Factory::createFoo(-1);
> print_r($a);
>
>
> This will generate the correct output in all three version I've tried:
>
> error Object
> (
> [msg] => Foo does not like negatives
> )
>
> The other upside benefit is that you can easily use a method that accepts
the
> type of object to create, and parameters, etc. Suggested reading are the
book
> "Design Patterns: Elements of Reusable Object-Oriented Software" and the
web
> site: http://hillside.net/patterns/
>
> > Thanks,
> >
> > Matt Friedman
>
> HTH. Will also look at your bug report and proceed accordingly.
>
> Cheers.
>
> =====
> --- Jesus M. Castagnetto (jcastagnetto@yahoo.com)
>
> Research:
> http://metallo.scripps.edu/
> Personal: http://www.castagnetto.org/
>
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
> http://mailplus.yahoo.com
>
=====
--- Jesus M. Castagnetto (jcastagnetto@yahoo.com)
Research:
http://metallo.scripps.edu/
Personal: http://www.castagnetto.org/
__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com