NYCPHP Meetup

NYPHP.org

[nycphp-talk] class design question

Hans Zaunere hans at nyphp.org
Fri Jul 11 15:28:11 EDT 2003



David Mintz wrote:
> I'm rewriting my PHP/MySQL application that manages the scheduling of
> court interpreters (for our office here in the federal court downtown).
> It's hideous spaghetti and I want to do it over with Smarty and Pear and
> OOP.
> 
> There are entities like Judge and User and Event and Interpreter that will
> merit their own objects and be persisted in the database.
> 
> Suppose you have a class User. Would you give it a save() method that
> writes its state to the database? And if so, do you pass this method a
> Pear $db object as argument, or does it already possess its own $db object
> (which perhaps you handed it at instantiation time)?

Yeah; I did something similar to this for auditing - I had an Expose method that would return a neat array of sorts that I could then save to file, db etc.

I typically have each class or base class make a db connection in the constructor.  I've found it convienent for the class to expect a named db link in the global space and use that.  If it doesn't find it, it'll do some predictable action, ie fail or create it.

> ... OR ...
> 
> Would you have write a separate DB class -- one that extends the Pear DB
> perhaps -- with methods like saveXXX() for each of your classes?

Ehh, that feels clunky IMHO.

H




More information about the talk mailing list