Re: Contributing / DB_Object
| From: | Mika Tuupola | Date: | Mon, 29 Apr 2002 13:45:54 +0000 |
| Subject: | Re: Contributing / DB_Object | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-5885@lists.php.net to get a copy of this message | ||
On Mon, 29 Apr 2002 fabien.marty@free.fr wrote:
> When you want to save a real object (with properties, methods...) in
> a database, I think you have two possibilities :
I've been playing with a bit similar idea, but just the other
way:
Since probably one of the most usual task people do with their
php scripts is to store and retrieve data from a database, you
could make one generic class for that task. I also had the idea
of calling it just DB_Object or Object_Generic. Since the
most tedious job is to write all getXXX() and setXXX()
methods you could simply just read in all the columns from the
table and create the set() and get() methods runtime.
However this is the problem. I havent yet found a way to
add methods to classes runtime.
The class should also only have just one simple save() method
for storing data. This same method is used both when updating
and inserting data.
Let's say if you had a table like
CREATE TABLE foo (
id INTEGER UNSIGNED NOT NULL.
bar VARCHAR(255),
baz TEXT
)
You would just create new DB_Object with something like:
$o = new DB_Object($dbh, 'foo');
and the $o would have methods created runtime which are simply:
getBar() {
return($this->bar)
}
setBar($input) {
$this->bar = $input;
}
etc... + and the save method.
DB_Object would be just a building block for real classes which
extend the DB_Object. Simply returning a property is most of the
times enough so you allready have the get / set methods ready. For
those get methods which need parameters you just overwrite them in
the extending class.
--
Mika Tuupola http://www.appelsiini.net/~tuupola/