Re: Contributing / DB_Object

From: Date: Mon, 29 Apr 2002 19:07:00 +0000
Subject: Re: Contributing / DB_Object
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-5901@lists.php.net to get a copy of this message
On Mon, 29 Apr 2002 fabien.marty@free.fr wrote: > to generate your get and set methods, what about using : > function genericGet($field) > { > return $this->{$field}; > } > function genericSet($field, $value) > { > $this->{$field} = $value; > } Set works but get wont. What if in the extended class you need to pass a parameter, say a format string to something like get('date', ). Now you have to rewrite the whole genericGet() and now just getDate(). > I you don't like this solution (there are some reasons), > the only way (i think) is to make a little PHP script which > generate the php code you want. Then, you make a copy/paste > to add these trivial methods to the rest of your class. Well how do you copypaste runtime? ;) > For the save() method, I use a global variable > $db_connection (the DB connection) and my save() method > is for example : IMO dbh in a global variable is bad. What if you have several connections to different databases? It's better to pass the dbh as a parameter to the constructor or factory method. > // to save changes > function save() > { > global $db_connection; > DB_Object::update($db_connection, "foo", "id=$this->id", $this, > "id"); > // to insert (new record) into database > function insert() > { > global $db_connection; > DB_Object::insert($db_connection, "foo", $this); Why have different methods for updating or inserting? Much simpler is just to have one save() method. If the object has an id then you know it allready exist in the database, if not then the object was created from data outside of databaase (say, submitted from a form or something) and then you have to an insert. -- Mika Tuupola http://www.appelsiini.net/~tuupola/

« previous php.pear.dev (#5901) next »