What are soem use cases for breaking a model into one base class and multiple dereived classes?


I am reading Yii Book by Larry Ullman and there is a tip in chapter 5 "working with models" that a model can be broken into one base class and multiple derived classes.

What are some use cases where this would be beneficial?

Is there any extension using this approach?

We are using single model class for our project at work, so wanted to learn bit more if new approach can help us.

Thanks for help.


You will normally put functions that are shared by all models in a base class and code that is unique to each model in the individual model classes (or behaviours).

Examples of functions shared by all models could be:

  • functions converting dates/times from DB format to the user’s preferred format and back to DB format for updating;

  • functions constantly filtering records based on user-permissions in defaultScope/beforeSave/beforeDelete;

  • functions handling errors that were raised in any of the individual models; etc.

I think one instance where you may want to take advantage of this is when working with modules. Your modules may need to reference some base models your application has defined, but these modules may also require module-specific code to be attached to the models, so extending them inside your module would be one way of achieving this.

Thanks for replies.

I think I have to go through some modules to see implementations.