0

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?

2 Answers 2

5

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

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

Sign up to request clarification or add additional context in comments.

9 Comments

it doesn't have to be the first line, also '::'
"first line of subclass" doesn't need to be the first, but must be with in the function and not the class
@Jens: why the first line, can't we override a function and call parent at any point of its execution?
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.
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.
|
1

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;
    }
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.