Re: calling parent class method from the outside
| From: | admin | Date: | Mon, 02 Jul 2007 18:09:18 +0000 |
| Subject: | Re: calling parent class method from the outside | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-258048@lists.php.net to get a copy of this message | ||
Colin Guthrie wrote:
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()) ...; [snipped]
I agree with Jochem that if you find you need to do this, then you're probably not designing the code correctly.OK, here we go: Propel in Symfony uses generated model classes representing DB rows, stub classes inheriting from them ready to be used (such as below), and accessors representing data columns: class Foo extends BaseFoo {
public function setBar($value)
{
$this->doSetColumn(FooPeer::BAR, $value, __FUNCTION__);
}
public function setBaz($value)
{
$this->doSetColumn(FooPeer::BAZ, $value, __FUNCTION__);
}
public function setXyzzy($value)
{
$this->doSetColumn(FooPeer::XYZZY, $value, __FUNCTION__);
}
// several more of these, and finally...
private function doSetColumn($colname, $value, $col_mutator)
{
/* setter does what it has to do and calls the parent
* version of self to do the real work of modifying
* state
*/
// ...
if ($something)
parent::$col_mutator($junk_to_trigger_modified);
// ...
parent::$col_mutator($value);
}
}
There are several models (tables) such as Foo, and they all have the
same body of doSetColumn(), so a logical next step was to factor that
method away _and_ leave most of the code intact, in which I've failed.
Once again, calling the parent version of a method "externally" is
allowed in C++ (and whoever said it was bad design should speak up to
Bjarne Stroustrup ;-)) Any such trick in PHP?