views:

51

answers:

2

Hi all,

I have a thing which I thought should work, but it doesn't.

I have several Controller_## classes, they all extend from Controller_Core. Each Controller_##-class has a public function Save(). Now I figured that I want to perform some other general checking (addslashes for each $_POST-var) and thought if I add a public function Save() to the Controller_Core it would be executed by default because the ##-class extends from it. However, this is not the case.

My question; is it possible what I'm trying to achieve? Or am I mistaken by thinking this would ever work?

Kind regards, Ben Fransen

+6  A: 

Call parent::Save() in the subclass version of the method.

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

Borealid
it doesn't have to be the first line, also '::'
Tom Dignan
"first line of subclass" doesn't need to be the first, but must be with in the function and not the class
jigfox
@Jens: why the first line, can't we override a function and call parent at any point of its execution?
Tom Dignan
Hmm okay, I knew that was possible but I kinda thought PHP would look in all inherited classes for equal functioncalls and then execute them. But thanks for your answer, I'll add the line in all Controller_##-classes.
Ben Fransen
Edited. It's just that the intuitive understanding is that the parent does something, and then the child extends on what the parent *has* done. So `parent::Save()` usually comes first.
Borealid
@Ben: the whole point of overriding a function is, that you can change the way a function works in child classes. So you must tell PHP when you don't want to replace the function, but only extend it by calling it's parent function explicitly
jigfox
@Borealid: Or the child can do some preparatory work, and pass control to parent afterwards.
Piskvor
@Piskvor thank you..The idea of subclassing is not strictly to modify what the parent 'has done' but to re use the parent in a more domain specific way: that said, you might want to override some functions and not even call parent. Some you might want to call parent conditionally. Some right up front, as you've suggested.Thinking it 'makes sense' to just call it first limits your flexibility as an OO program designer significantly.
Tom Dignan
@Tom Dignan: I understand the different ways inheritance and virtual methods can be used. It's just that the case I thought of first is the one where a child class *extends* the functionality of the parent.
Borealid
+1  A: 

Or you could apply refactoring to extract common behavior to your core class :

class Controller_Core {

    public function save() {
        if ( ! $this->_validateInfo() ) {

            return false;
        }

        return $this->_doSave();
    }

    protected function _validateInput() {
        //-- do stuff

        return true;
    }

    protected function _doSave() {
        //-- do stuff

        return true;
    }

}

You write the specific code in the children classes, as in :

class Controller_Extended extends Controller_Core {

    protected function _doSave() {

        //-- a new return
        return true;
    }
} 
JonG