dan-lee
dan-lee

Reputation: 14492

Force to call parent::preExecute() in a symfony action

I have a similar structure like this:

Parent class

abstract class parentActions extends sfActions
{
   // overloaded from sfActions
   public function preExecute()
   {
     // do some stuff before every action
   }

}

Child class

class someActions extends parentActions
{
  public function preExecute()
  {
     // do some more stuff
     parent::preExecute();
  }
}

Now my question is: How can I enforce a call to parent::preExecute() in the child method which overwrites it?

Is there maybe some other way in symfony I don't know yet (another method which doesn't overloading or something)?

The parent method needs to be called, or otherwise functionality is broken!

Upvotes: 1

Views: 1578

Answers (3)

Francis Manoj Fernnado
Francis Manoj Fernnado

Reputation: 262

put it in the abstract class __constructor()

Upvotes: 0

Ja͢ck
Ja͢ck

Reputation: 173562

This is exactly what inheritance should do; the child specializes a method while retaining the pre and post conditions. This makes it possible to substitute a parent class with a child class.

Perhaps you could declare empty hooks in the parent class that get implemented in the child class and add final to the preExecute() declaration to prevent accidental overrides.

Upvotes: 1

Dennis Haarbrink
Dennis Haarbrink

Reputation: 3760

There is no way to enforce it. It is the sole responsibility of the child class to decide whether or not to call the parent method.

Upvotes: 1

Related Questions