0

Whats the best practice in overriding methods? Especially if we need to add another param?

This is not E_STRICT compliant (adding $soft as second param):

public function delete($id, $soft = false, $cascade = true) {
    if ($soft) {
        return $this->_softDelete();
    }
    return parent::delete($id, $cascade);
}

Resulting in:

Declaration of Conversation::delete() should be compatible with that of Model::delete()

I know that one shoudn't override methods this way (adding-parameters-to-overriden-method-e-strict-observation).

but If one has to, how would one proceed? (without having to remove E_STRICT) The basic idea was to intercept the normal delete calls without having to rewrite all occurrences of this model method call.

Community
  • 1
  • 1
mark
  • 21,691
  • 3
  • 49
  • 71
  • Did you try use `beforeDelete` method in AppModel? Maybe it's a good alternative to override the core `delete()`. – Paulo Rodrigues Mar 02 '12 at 12:51
  • well, my question is more a generic one (the above is only a concrete use case as example). there are lots of other places in the code where this problem occurs in E_STRICT mode. So I will have to address all of them somehow. – mark Mar 02 '12 at 13:07

1 Answers1

1

It's either E_STRICT compatibility, or modyfing function signatures. You can't have both.

The solution is usually to use composition instead of inheritance, that is to wrap the object which behaviout you'd like to modify within a new class with different signatures.

Mchl
  • 61,444
  • 9
  • 118
  • 120