Re: An overloading question [message #174523 is a reply to message #174492] |
Wed, 15 June 2011 16:02 |
Michael Fesser
Messages: 215 Registered: September 2010
Karma:
|
Senior Member |
|
|
.oO(sheldonlg)
> On 6/14/2011 2:32 PM, Denis McMahon wrote:
>
>> This code:
>>
>> <?php
>>
>> abstract class A {
>>
>> public function x() {
>> print "x() In class A\n";
>> $this->y();
>> }
>>
>> public function y() {
>> print "y() In class A\n";
>> }
>>
>> public function a() {
>> print "a() In class A\n";
>> }
>>
>> }
>>
>> class B extends A {
>>
>> public function y() {
>> print "y() In class B\n";
>> }
>>
>> }
>>
>> print "instantiate\n";
>>
>> $x = new B();
>>
>> print "call function\n";
>>
>> $x->x();
>>
>> ?>
>>
>> gives:
>>
>> instantiate
>> a() In class A
>> call function
>> x() In class A
>> y() In class B
>>
>> Not sure, though, if that solves your problem or not?
>>
>> Rgds
>>
>> Denis McMahon
>
> I modified B to give it an explicit constructor. It still didn't work.
> Why did your example print out that last line. It was never called.
The command $x->x() would call B::x(), but there's no such method. So
the inherited method A::x() is called, which then calls $this->y(). And
since $this refers to an instance of class B, B::y() is called, leading
to the last line of output.
Micha
|
|
|