Overwriting functions in PHP?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lars Plessmann

    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
  • Garp

    #2
    Re: Overwriting functions in PHP?


    "Lars Plessmann" <Lars.Plessmann @gmx.de> wrote in message
    news:c72lhu$hi7 $04$2@news.t-online.com...[color=blue]
    > Is ist possible, to overwrite a function in PHP by calling the parent
    > function that has the same name?[/color]
    <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


    Comment

    • Adriaan

      #3
      Re: Overwriting functions in PHP?

      "Lars Plessmann" wrote[color=blue]
      > $this->funcDisplay( );
      > // should call the func of the parent class![/color]

      No, this is called recursion, and like you expected this calls itself, until
      it runs out of memory.
      [color=blue]
      > But how can I tell this the PHP compiler to use the upper class?[/color]

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

      Adriaan


      Comment

      • Lars Plessmann

        #4
        Re: Overwriting functions in PHP?

        Lars Plessmann wrote:
        [color=blue]
        > 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[/color]

        it works!!
        many thanks :-)

        Comment

        Working...