Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7672385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:16:24+00:00 2026-05-31T16:16:24+00:00

I feel there is probably a well trodden design pattern to handle situations like

  • 0

I feel there is probably a well trodden design pattern to handle situations like the one below, but I’m just experienced enough to have come across it!

Basic Problem

The escense of the problem is say I have myClass with a method myMethod and I have a class extClass that extends myClass and I wish myMethod to have slightly different behaviour in extMethod then I can overwrite the method in the extended class, but then I end up having duplicated code.

Specific details

The specific problem that I’m trying to deal with is using MVC where I have a base model class which defines how inserts and deletes are performed. For some database tables I have implemeneted record versioning which is done with a class version_model which extends the base class and overwrites the insert/delete methods to add in triggers to create the record versions. This is all fine except that for some specific database table models I want to define custom insert/delete methods that will automatically handle inserts to nomalized tables (e.g. person table > addresses). This is ok except if I have to copy code from either the version_model or the base_model depending on which one it extends.

I was thinking this could maybe be solved with hooks, so my version_model and base_model insert/delete methods called ‘before_insert()’ and ‘after_insert()’ methods then I could add the additional functionality without duplicating the base functionality. Is there a better way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:16:25+00:00Added an answer on May 31, 2026 at 4:16 pm

    Separate out the code that differs from the code that’s common, keep the common code in the superclass and call the parent’s method from the subclass e.g. (php)

    protected function subclass_method() {
        $data = parent::superclass_method();
    
        // do stuff with data
    }
    

    The hooks approach works too, in which case, you are doing more or less the same, but putting the logic into the superclass, which is an example of the template method pattern.

    The usual alternative would be the strategy pattern, where you swap out the behaviours as objects, but this is more suited to situations where the number of combinations of methods you need would cause a very complex class hierarchy and may be overkill. Template or calls to parent methods are fine for this IMO. Try breaking down the process into the smallest steps you can. If there are several steps that vary across classes, use template/hooks. If only one does, use a function in the parent class for that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like there's a simple solution to this, but I'm not really sure
I just had the jQuery epiphany the other day and still feel like there
I feel stupid for asking, but there must be a one liner that does
I feel like this is something I should already know, but I'm just not
I've been thinking of implementing this system, but can't help but feel there's a
I feel like I'm taking crazy pills here. Usually there's always a million library
There probably is a fairly simple and straight-forward answer for this, but for some
In my heart, I feel that there must be a super simple recursive solution
Feel free to close and/or redirect if this has been asked, but here's my
First, I understand the how of iteratees, well enough that I could probably write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.