470,815 Members | 1,316 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

Overwriting functions in PHP?

Is ist possible, to overwrite a function in PHP by calling the parent
function that has the same name?
I have a classA and a classB with a method funcDisplay():

----------------------------------

classA {

function funcDisplay()
{
print "line 1A";
print "line 2A";
}

}

-----------------------------------

classB extends classA
{

functionm funcDisplay
{
$this->funcDisplay();
// should call the func of the parent class!

print "line 1B";
print "line 2B";
}

}
When I test this, I get an memory failure and a fatal error message from
windows. I think $this->funcDisplay is an endless loop by calling
itself. But how can I tell this the PHP compiler to use the upper class?

I want to call from funcDisplay() from classB the funcDisplay() in
classA, that I get the inherited function of the parent class like the
following result:

----
line 1A
line 2A
line 1B
line 1B
----

I thank you in advance,

Lars
Jul 17 '05 #1
3 2920

"Lars Plessmann" <La************@gmx.de> wrote in message
news:c7*************@news.t-online.com...
Is ist possible, to overwrite a function in PHP by calling the parent
function that has the same name?

<snip>

Yes, use parent: http://uk.php.net/manual/en/keyword.parent.php.

The example given is:
<?php
class A {
function example() {
echo "I am A::example() and provide basic functionality.<br
/>\n";
}
}

class B extends A {
function example() {
echo "I am B::example() and provide additional
functionality.<br />\n";
parent::example();
}
}

$b = new B;

// This will call B::example(), which will in turn call A::example().
$b->example();
?>
HTH
Garp
Jul 17 '05 #2
"Lars Plessmann" wrote
$this->funcDisplay();
// should call the func of the parent class!
No, this is called recursion, and like you expected this calls itself, until
it runs out of memory.
But how can I tell this the PHP compiler to use the upper class?


See http://php.net/keyword.parent

Adriaan
Jul 17 '05 #3
Lars Plessmann wrote:
Is ist possible, to overwrite a function in PHP by calling the parent
function that has the same name?
I have a classA and a classB with a method funcDisplay():

----------------------------------

classA {

function funcDisplay()
{
print "line 1A";
print "line 2A";
}

}

-----------------------------------

classB extends classA
{

functionm funcDisplay
{
$this->funcDisplay();
// should call the func of the parent class!

print "line 1B";
print "line 2B";
}

}
When I test this, I get an memory failure and a fatal error message from
windows. I think $this->funcDisplay is an endless loop by calling
itself. But how can I tell this the PHP compiler to use the upper class?

I want to call from funcDisplay() from classB the funcDisplay() in
classA, that I get the inherited function of the parent class like the
following result:

----
line 1A
line 2A
line 1B
line 1B
----

I thank you in advance,

Lars


it works!!
many thanks :-)
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Ellixis | last post: by
1 post views Thread by chris kittell | last post: by
9 posts views Thread by Robert | last post: by
19 posts views Thread by tweak | last post: by
1 post views Thread by Sargas Atum | last post: by
11 posts views Thread by Steven Jones | last post: by
27 posts views Thread by Jason | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.