Re: calling parent class method from the outside

From: Date: Mon, 02 Jul 2007 15:15:19 +0000
Subject: Re: calling parent class method from the outside
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-258043@lists.php.net to get a copy of this message
admin wrote:
Inside the body of method foo() you can of course use syntax like parent::foo(). But is there a way to call the parent version of obj->foo() outside the class? That kind of syntax is allowed in C++, for example: Aclass a; if (a.Aparent::foo()) ...; Some contrived example to illustrate the point: class AParent { public function foo() { .. } } class A extends AParent { public function foo() {
    doit($this, __CLASS__, __FUNCTION__);
} } function doit($obj, $classname, $funcname) { if (...)
//    $obj->classname_parent::$funcname();
} Thanks.
To use Richards example, but with a little different twist for accessing the parent methods/properties class A {
    public __construct()
    {
        // ...
    }
    public function foo ()
    {
        // ...
    }
} class B extends A {
    // Constructor
    public function __construct ()
    {
        parent::__construct();
    }
} // And then... $b = new B(); $b->foo(); This will bring all methods/properties from class A into class B. You can choose to override class A methods/properties when you define class B. But the idea here is that only need to write the methods/properties once in the parent class, then you can extend your class with class A and have all the methods/properties available to you within your current class. It is good to practice the DRY principle here. Note: Richard Heyes thanks for the code snippet -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare

« previous php.general (#258043) next »