Re: PEAR DB problem

From: Date: Mon, 03 Feb 2003 14:53:11 +0000
Subject: Re: PEAR DB problem
References: 1 2  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-3580@lists.php.net to get a copy of this message
Am Mon, 3 Feb 2003 14:37:42 +0100 (CET) schrieb Hrabcak Rado <hrabcak@phf.euke.sk>: > On Mon, 3 Feb 2003, Markus Wolff wrote: > > >...and this is what you´re supposed to do: > > > >$this->db = DB::connect("pgsql://user@localhost/hrabcak",true); > > > >With "new DB" you just get an object of the generic DB base class, > >which doesn´t do a lot. Using the factory method DB::connect() gives > >you an object of a driver class specific to the database you´re using > >(in this case, PostgreSQL). > > So this is only because a specific situation of PEAR::DB, isn't it? > Because I normally use $this->variable = new Class; and it works. It depends on the class you´re using. Objects of simple classes are mostly created using "new". However, if the class is part of a larger package with more than one variant of the same type of class, chances are good that you´ll need to use a factory method. Another good example of this is the PEAR::Mail package. However, sometimes even simple classes use factory methods to check the parameters passed to the constructor. Based on the validity of the parameters, the factory method will either return an object of the same class, or an error object giving some more information about what went wrong (i.e. a PEAR_Error object). If you want more information about the use of factory methods and other useful design patterns, visit this excellent site: http://www.phppatterns.con/ Regards, Markus P.S.: Damn, I´ve just seen that the factory pattern is not explained there... but anyway, you should have a look at the site no matter what - it will help you to better understand coding of complex classes/packages. -- *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

« previous php.pear.general (#3580) next »