You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Steve Hay <st...@uk.radan.com> on 2003/04/23 19:09:06 UTC

Subclassing Apache::Request

Hi,

I'm trying to write a sub-class to Apache::Request and I find that I 
can't inherit its constructor because it always blesses the new object 
into Apache::Request itself rather than the invocant class, i.e. the 
program:

    package Foo;
    use Apache::Request;
    BEGIN {
        our @ISA = qw(Apache::Request);
    }

    package main;
    MAIN: {
        my $r = Apache->request();
        my $foo = Foo->new($r);
        print "Content-type: text/html\n\n";
        print "foo is-a ", ref($foo), "\n";
    }

prints "foo is-a Apache::Request" instead of "foo is-a Foo".

Is this intentional or a bug?

I can, of course, write my own constructor in the sub-class to simply 
re-bless the new object:

    sub new {
        my($class, @args) = @_;
        return bless $class->SUPER::new(@args), $class;
    }

but it would be better not to have to do this unless there is a good 
reason for it.

Steve


Re: Subclassing Apache::Request

Posted by Joe Schaefer <jo...@sunstarsys.com>.
Steve Hay <st...@uk.radan.com> writes:

[...]

>     package Foo;
>     use Apache::Request;
>     BEGIN {
>         our @ISA = qw(Apache::Request);
>     }
> 
>     package main;
>     MAIN: {
>         my $r = Apache->request();
>         my $foo = Foo->new($r);
>         print "Content-type: text/html\n\n";
>         print "foo is-a ", ref($foo), "\n";
>     }
> 
> prints "foo is-a Apache::Request" instead of "foo is-a Foo".
> 
> Is this intentional or a bug?

I think this behavior is caused by the typemap (T_APROBJ) for 
Apache::Request objects.  Don't know if its a bug or a necessary
consequence of having Apache::Request::new() implemented in XS 
instead of Perl.

> I can, of course, write my own constructor in the sub-class to simply
> re-bless the new object:
> 
>     sub new {
>         my($class, @args) = @_;
>         return bless $class->SUPER::new(@args), $class;
>     }
> 
> but it would be better not to have to do this unless there is a good 
> reason for it.

That should work ok. If it doesn't, you could try something like this:

  sub new {
    my ($class, $r) = @_;
    my $apreq = Apache::Request->new($r);
    return bless { r => $apreq }, $class;
  }

Using 'r' or '_r' as an Apache::Request attribute is documented
to be safe for subclassed objects.

-- 
Joe Schaefer

Re: Subclassing Apache::Request

Posted by Geoffrey Young <ge...@modperlcookbook.org>.

Steve Hay wrote:
> Hi,
> 
> I'm trying to write a sub-class to Apache::Request and I find that I 
> can't inherit its constructor because it always blesses the new object 
> into Apache::Request itself rather than the invocant class

there are a few examples in the cookbook that subclass Apache::Request,

http://www.modperlcookbook.org/code/ch13/Cookbook/CookieAuthentication.pm
http://www.modperlcookbook.org/code/ch10/Cookbook/TransformRequest.pm

to name just two.  recipe 10.8 is specifically about subclassing 
Apache::Request.

HTH

--Geoff