Call an overridden method in parent class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Summary: How to call an overridden non static method in the parent class ?

Calling an overridden static method is easy, just go: parent::MethodName().

The point is that I want to have the same 'output' method name in both classes,
so that whatever has one of them just calls $xxx->output() and it will work.

I want the method in the child class to use what the parent class has and just
add a bit to it.  I don't want to duplicate a lot of code in class B that is
already in class A.

The way that I have a (hopefully temporary) workaround is to in class A have a
method do_output() that is called by A::output() and B::output(). That works,
but is clunky.

Is there a neat way of doing what I want ?



class A {
    private $v_a;

    // Does all sorts of things and returns a value
    function output() {
        return array('a' => $this->v_a);
    }
}

class B extends A {
    private $v_b;

    // Takes what output() in class A returns, adds some more and returns that
    function output() {
        $ret = parent::output();
	$ret['b'] = $this->v_b;
        return $ret;
    }
}


$var_a = new A;

$var_b = new B;

print_r($var_a->output());

// I want to see the value of v_a and v_b:
print_r($var_b->output());

-- 
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer.
+44 (0) 787 668 0256  http://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php
#include <std_disclaimer.h>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux