thunders has asked for the wisdom of the Perl Monks concerning the following question:
and then initialize tickets like so:package MyApp::Ticket; use strict; sub new { my ($class,%args) = @_; my $self = bless { _time => $args{'time'}, _trade_ref => $args{trade_ref}, _adr_buyer_bank => $args{adr_buyer_bank}, _adr_seller_bank => $args{adr_seller_bank}, _seller_pays => $args{seller_pays}, }, $class; return $self; } sub load { my ($class,$dbh,$ticket_number) = @_; my $sql = "SELECT * FROM tickets WHERE id = ?"; my $sth = $dbh->prepare($sql) or die $dbh->errstr; $sth->execute($ticket_number); my $row = $sth->fetchrow_hashref; my $self = bless { _time => $row->{'time'}, _trade_ref => $row->{trade_ref}, _adr_buyer_bank => $row->{adr_buyer_bank}, _adr_seller_bank => $row->{adr_seller_bank}, _seller_pays => $row->{seller_pays}, }, $class; return $self; }
I'm not quite sure if that would work, and it seems kind of messy. Another idea I had was to define separate constructors in DBTicket and NewTicket classes, and simply inherit all the common methods from the regular Ticket class. Perhaps that would be cleaner? Any advice would be appreciated.my $new_ticket = new MyApp::Ticket( time=>time(),trad_ref=>34567, adr_buyer_bank => 'blah', adr_seller_bank =>'blah, seller_pays => 99.99 ); my $old_ticket = MyApp::Ticket->load(12345);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: how do i implement alternate constructors for an object
by kilinrax (Deacon) on May 07, 2003 at 17:07 UTC | |
|
Re: how do i implement alternate constructors for an object
by VSarkiss (Monsignor) on May 07, 2003 at 19:09 UTC | |
|
Re: how do i implement alternate constructors for an object
by LanceDeeply (Chaplain) on May 07, 2003 at 20:59 UTC | |
|
Re: how do i implement alternate constructors for an object
by nite_man (Deacon) on May 07, 2003 at 21:08 UTC | |
|
Re: how do i implement alternate constructors for an object
by shemp (Deacon) on May 07, 2003 at 18:06 UTC |