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 #174501 is a reply to message #174492] Wed, 15 June 2011 03:41 Go to previous messageGo to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On Tue, 14 Jun 2011 18:27:20 -0400, sheldonlg 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?

> Why did your example print out that last line. It was never called.
> Why did it use it as a constructor? That doesn't seem to make sense.

OK, the calls as I understand it in my code.

First I print "instantiate"

Next, "$x = new B();" causes $x to be created, which instantiates b,
which extends a, which causes A::a() to be called as a constructor
function, which prints "a() In class A".

Then I print "call function"

Finally, "$x->x();" calls A::x() which prints the line "x() In class A"
and then calls $this->y() which finds B::y() which prints the line "y()
In class B".

I'm not sure if your complaint is about the sequence of call of the
constructor functions during instantiation of $x, or the call chain after
instantiation, which is why I gave the functions called after
instantiation different names (x and y) to the constructor function in
class A.

I believe that in your code, A::a() was being called as a constructor at
instantiation, and A::b() was calling B::a() after instantiation. That's
certainly suggested by:

<?php

abstract class A {
public function a() {
print "a() In class A\n";
}
public function b() {
print "b() In class A\n";
print "b() In class A calling \$this->a()\n";
$this->a();
print "b() In class A returned from \$this->a()\n";
}
}

class B extends A {
public function a() {
print "a() In class B\n";
}
}

print "instantiate\n";
$x = new B();
print "call function\n";
$x->b();
?>

giving:

instantiate
a() In class A
call function
b() In class A
b() In class A calling $this->a()
a() In class B
b() In class A returned from $this->a()

Note that A::a() is called as constructor during instantiation of $x
A::b() calling $this->a() after instantiation of $x calls B::a() and not
A::a().

Your initial complaint was about A::a() being called, is it possible that
the A::a() call that you observed was from instantiation and not the call
to $x->b()?

Rgds

Denis McMahon
[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: Fri Sep 20 17:28:58 GMT 2024

Total time taken to generate the page: 0.04143 seconds