Re: Re: calling parent class method from the outside
| From: | Jim Lucas | Date: | Mon, 02 Jul 2007 18:57:37 +0000 |
| Subject: | Re: Re: calling parent class method from the outside | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-258050@lists.php.net to get a copy of this message | ||
admin wrote:
Colin Guthrie wrote:admin wrote:[snipped]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()) ...;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)What are you wanting to test for?
parent::$col_mutator($junk_to_trigger_modified);does this setup the next call to $col_mutator() ? when you pass the value?
This is a little different then what you are trying above, but maybe it is close to what you are looking for: <?php class FooPeer { function BAR() { return 'BAR'; } function BAZ() { return 'BAZ'; } function XYZZY() { return 'XYZZY'; } } class BaseFoo {// ...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?
function setBar($value) {
echo "BaseFoo::setBar({$value});\n";
}
function setBaz($value) {
echo "BaseFoo::setBaz({$value});\n";
}
function setXyzzy($value) {
echo "BaseFoo::setXyzzy({$value});\n";
}
}
class Foo extends BaseFoo {
function setBar($value) {
$this->doSetColumn(FooPeer::BAR(), $value, __FUNCTION__);
}
function setBaz($value) {
$this->doSetColumn(FooPeer::BAZ(), $value, __FUNCTION__);
}
function setXyzzy($value) {
$this->doSetColumn(FooPeer::XYZZY(), $value, __FUNCTION__);
}
function doSetColumn($colname, $value, $col_mutator) {
parent::$col_mutator($value);
}
}
$Foo = new Foo();
echo "<pre>";
$Foo->setBar('my value for bar');
$Foo->setBaz('my value for baz');
$Foo->setXyzzy('my value for xyzzy');
--
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