RE: [PHP] How to call a member from the base class?
| From: | Neil Kimber | Date: | Mon, 03 Sep 2001 12:31:00 +0000 |
| Subject: | RE: [PHP] How to call a member from the base class? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-65738@lists.php.net to get a copy of this message | ||
No! This won't work. There are two ways in which you can get an overridden
method to call the method in its super-class, these are by using the
following constructs:
1) parent::
2) <classname>::
So, you could do the following:
1)
class B extends A {
function something() {
// some other actions
parent::something(); // calls A::something
}
}
or
2)
class B extends A {
function something() {
// some other actions
A::something(); // calls A::something
}
}
I've experienced PHP getting confused when using the parent:: construct. If
you have an object that is 3 levels deep in sub-classing and each sub-class
overrides the same method then the parent:: construct in the level 2 object
sometimes calls the level objects method - ad infinitum. This seems to be a
bug with the parent:: construct.
i.e.
class A{
function foo(){
print "A";
}
class B extends A{
function foo(){
parent::foo();
print "B";
}
class C extends B{
function foo(){
parent::foo();
print "C";
}
$bar=new C();
$bar->foo();
// Output would be:
CBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB..............
because parent::foo() in class B incorrectly resolves to B::foo().
So, I normally use the explicit class name construct, giving:
class A{
function foo(){
print "A";
}
class B extends A{
function foo(){
A::foo();
print "B";
}
class C extends B{
function foo(){
B::foo();
print "C";
}
$bar=new C();
$bar->foo();
// Output would be:
CBA
Hope this helps.
Neil
-----Original Message-----
From: Alexander Deruwe [mailto:aderuwe@deruwe.be]On Behalf Of Alexander
Deruwe
Sent: 03 September 2001 14:51
To: A. op de Weegh; php-general@lists.php.net
Subject: Re: [PHP] How to call a member from the base class?
On Monday 03 September 2001 09:47, A. op de Weegh wrote:
> How do I make sure in the something() member of class B, that the parent
> class member something() is also called? Can I do this:
Hello there, person with the same name as self, :)
class B extends A {
function something() {
// some other actions
$this->something(); // calls A::something
}
}
I've used this code for calling base class constructors. These ofcourse have
different names, while your functions are both called the same. I don't
think
that should make any difference, though.
ad.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net