Re: Constructors as factory methods
| From: | (Stig Sæther Bakken) | Date: | Fri, 17 Aug 2001 07:26:05 +0000 |
| Subject: | Re: Constructors as factory methods | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-1583@lists.php.net to get a copy of this message | ||
[Mika Tuupola <tuupola@appelsiini.net>]
> Is there any concensus on can a class constructor act as
> a factory method?
>
> For example if I have a classes Car Car_Lada and Car_Porche,
> I would like to make the constructor return the concrete
> class like:
>
> $c = new Car("Lada")
To do this you need to overwrite $this in the constructor, and this is
only a side-effect in Zend 1.0, and probably won't survive into Zend
2.0.
> instead of using
>
> c= new Car::Factory("Lada")
>
> However looking at other PEAR classes they all use a static
> factory method call to return the concrete object. Which way
> is preferred?
This is the preferred way.
- Stig
--
Stig Sæther Bakken <ssb@alltheweb.com>
Fast Search & Transfer ASA, Trondheim, Norway