Re: Preferred method for using factory pattern
| From: | Markus Wolff | Date: | Thu, 20 Feb 2003 15:41:25 +0000 |
| Subject: | Re: Preferred method for using factory pattern | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-13499@lists.php.net to get a copy of this message | ||
Am Thu, 20 Feb 2003 16:03:43 +0100 schrieb Demian Turner <demian@muse23.com>:
> What is the preferred method within PEAR for using the factory pattern?
>
> In Mail it's used like:
>
> $mail_object =& Mail::factory('sendmail', $params);
>
> while in Auth, _factory() is private and instantiated like:
>
> $a = new Auth("DB","mysql://martin:test@example.com/test");
>
> I'm writing menu generation class and am guessing the latter is preferred ?
Hi Demian,
I guess it depends on what you want to achieve, but I prefer to have
the factory method public. Besides providing a comfortable interface
to a whole family of classes, factory methods in PHP have advantages
even if you don´t need to choose another class to instantiate:
A normal constructor in PHP can´t return anything except for an object
of its class. Using a factory methods even in places where you
normally wouldn´t need it in other languages allows you to check the
validity of the parameters given to the method and return an error
object if something went wrong. This way you can catch errors much
better.
Wherever I can, I use factory methods instead of using "new".
But that´s just my 0.02 EUR ;-)
Regards,
Markus
--
*21st Media* | Consulting, Konzeption, Produktion für die Bereiche:
Markus Wolff | Internet, Intranet, eCommerce, Content Management,
Hamburg,Germany | Softwareentwicklung, 3D-Animation, Videostreaming
http://21st.de | Tel. [+49](0)40/6887949-0, Fax: [+49](0)40/6887949-1