
budman
User
Apr 3, 2012, 9:00 PM
Views: 2396
|
|
Re: [ChopperCharles] private and protected methods
|
|
|
Okay, I've changed my code around, but there's still a problem: unless ( (caller)[0]->isa( ref($self) ) ) { $self->__SetLastError("Access to protected method denied."); return -1; } This doesn't work when the caller is a subclass. If I have class B that inherits from class A, $self will be B, but caller will be A. Since the subclass is inheriting from the parent, all the methods belong to the subclass, except those methods that are overridden. To access the parents versions, you must use SUPER. Even calling SUPER, as the TacoBell new method does, it still reports the caller as TacoBell. When I ran a test on the script provided, I see that is the case: # all the taco bell objects NEW -> Self: TacoBell Caller: TacoBell t = TacoBell->new(); Self: Caller: fooBar __foo -> t->iAmfooBar() success. Self: TacoBell Caller: main __foo -> t->__foo("Charles") failed. Self: TacoBell Caller: TacoBell (this is in the base class) __foo -> t->burrito() success. # the foobar objects NEW -> Self: fooBar Caller: main f = fooBar->new(); Self: fooBar Caller: main __foo -> f->__foo("d'oh") failed. Again, this is all in the perldoc object oriented tutorials.
(This post was edited by budman on Apr 3, 2012, 9:03 PM)
|