Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Thanked 0 Times in 0 Posts

    MVC : Dynamically adding functionnalities to a model class


    I am writing a basic PHP framework for my personal use. I have an abstract model class :
    PHP Code:
    abstract class Model {      
        public function 
    save() {
    /* Save data */

        public function 
    delete() {
    /* Delete a record */

    I create model classes extending the base Model class, for instance :
    PHP Code:
    class ArticleModel extends Model {   

    I would like to add functionalities to my models in a flexible manner and so that I would be able to reuse them (publication, trash bin, translation...). I already have a event system based on the observer pattern which allow me to attach listeners to an object. It's quite useful but I need the ability to add new operations to existing object. I am wondering what kind of approach/pattern would be more suitable.
    The decorator pattern allow to add dynamically functionalities to an object.

    PHP Code:
    class TrashBinDecorator {

    $model null;

        public function 
    __constructModel $model ) {
    $this->model $model;

        public function 
    delete() {
    // overriding the delete() method

        public function 
    restore() {
    // ...

    $article = new TrashBinDecoratorArticle::findOne) ); 
    But I use finders methods to create the model objets :
    PHP Code:
    $article Article::findOne);
    $articles Article::findAll(); 
    My idea is to use a configurable factory to create model instances :

    PHP Code:
    public static function findOne {
    // executing the database query
        // creating the model instance
    $model Factory::createget_classself ) );
    // ...
    return $model;

    The factory should be able to create a decorated instance using a configuration file :
    PHP Code:
    class Factory {
        public static function 
    create$className ) {
    $object = new $className();
    $decorators as $decorator ) {
    $object = new $decorator$object );

    If I would like to add a functionality to a model, I simply would have to modify a configuration file.

    What do you think of this approach ?
    Last edited by Neuromancer2; 03-29-2010 at 05:02 PM.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts