FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » An overloading question
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: An overloading question [message #174523 is a reply to message #174492] Wed, 15 June 2011 16:02 Go to previous messageGo to previous message
Michael Fesser is currently offline  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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: check user log and redirect
Next Topic: use GET in include
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 09:24:30 GMT 2024

Total time taken to generate the page: 0.04227 seconds