Phoenix.pm: Inheriting from DateTime
Douglas E. Miles
perlguy at earthlink.net
Fri Feb 20 18:22:34 CST 2004
Douglas E. Miles wrote:
> Scott Walters wrote:
>
>> Hi Doug,
>>
>> Looks like you're going down a good road right now. Just have to go a
>> little
>> further.
>>
>>
>>> sub new
>>> {
>>>
>>> my $package = shift;
>>> my $class = ref($package) || $package;
>>
>>
>>
>> my %params = @_;
>> my $event_name = delete $param{event_name};
>>
>>
>>> # my $self = $class->SUPER::new(@_);
>>
>>
>> my $self = $class->SUPER::new(%params);
>>
>>
>>> bless($self, $class);
>>
>>
>>
>> $self->{event_name} = $event_name; # ram it in there forceibly
>>
>>
>>> return $self;
>>>
>>> } # END: new
>>
>>
>>
>> That's kind of ugly, between reblessing the object, stripping off
>> unexpected
>> parameters, and then mashing them into the object. I don't know of a
>> better
>> way to do this, though.
>
>
> <SNIP>
>
> Thanks Scott. That was exactly the approach I was going to take. I
> agree that its ugly. I was hoping I was missing something obvious. I
> guess that's what I'll do unless someone else comes up with a better
> idea. All my inheritance experience involves my own classes, so I've
> never run into this before.
>
Since I'm sure the suspense is killing all of you, here's what I ended
up with:
my @local_attributes =
(
qw
(
calendar
event_name
)
);
sub new
{
my $package = shift;
my $class = ref($package) || $package;
my %attributes = @_;
my %local_attributes;
@local_attributes{@local_attributes} =
delete(@attributes{@local_attributes});
my $self = $class->SUPER::new(%attributes);
bless($self, $class);
@{$self}{@local_attributes} = @local_attributes{@local_attributes};
$self->initialize;
return $self;
}
I did it this was, because I know that there will be more attributes
added later. Comments?
More information about the Phoenix-pm
mailing list