0

I have recently started to work on creating a MVC PHP private framework (I know, there is no classical MVC in PHP, you guys understand what I mean by MVC in PHP).

I have encountered a problem, I know that a single controller can load multiple models for specific tasks. That's ok, but what happens when my models should load the same libraries/dependencies? Eg:

<?php

abstract class Model {

  protected $db = null;
  protected $validator = null;
  protected $validations = null;

  protected function _startDb() {

    if ( $this->db === null ) {

      $this->db = new Db();
    }
  }

  protected function _startValidator() {

    if ( $this->validator === null ) {

      $this->validator = new Validator();
    }
  }

  protected function _startValidations() {

    if ( $this->validations === null ) {

      self::_startValidator();
      $this->validations = new Validations($this->validator);
    }
  }

  // I have more functions like these, as you can see, they load needed libraries.
}

// Now my models which they all need some of these libraries that are loaded in model class.

class Roles extendes Model {

  public function getRoles() {

    // Makes use of _startDb(), _startValidations()... 
  }
}

class Permissions extends Model {

  public functoin getPermissions() {

   // Also makes use of _startDb(), _startValidations()...
  }
}

Well, I find no problem with this code as long as my controller just load one model. But it's not clear to me, and this is what bothers me, if I load multiple models in one controller, then it means that each model have its own instance of each library.

I do not like that, because specially db queries will get slower.

So my question, what is the best way to keep things well organized and have one instance of each library and share them among multiple models.

Btw, I know about dependency injection, but some models would need to get like 4 libraries and thats feels wrong to me. And then I feel like the controller would have load all the libraries, and I do not know if thats ok.

Victor Tello
  • 161
  • 13
  • Err, inject the same `Db` instance into all the models ? – frz3993 Feb 09 '16 at 19:00
  • Multiple models would have to get `Db(), Validator(), Validations(), Session(), Rbac()` injected... That feels wrong. – Victor Tello Feb 09 '16 at 19:02
  • @VictorTello, this phrase "have one instance of each library and be able to load multiple models" seems to be confusing. You want to share single instances of `Db` , `Validator` among multiple models ? – RomanPerekhrest Feb 09 '16 at 19:08
  • @RomanPerekhrest Yes, I want just one instance of each library, and share those among multiple models. Sorry for my bad English. – Victor Tello Feb 09 '16 at 19:10
  • @VictorTello, just instantiate all the service objects(libraries) once before instantiating multiple model objects. Then pass them as dependency injection – RomanPerekhrest Feb 09 '16 at 19:36
  • @RomanPerekhrest So I should instantiate all of them in the controller. Is that ok? – Victor Tello Feb 09 '16 at 19:43
  • usually, a client is requesting one action from controller at a time. So you can set those service objects once within controller's `__construct` or `init` method – RomanPerekhrest Feb 09 '16 at 20:11
  • @RomanPerekhrest Yeah, I was thinking of that... Thanks for your help. – Victor Tello Feb 09 '16 at 20:26
  • I would recommend to look at this SO post http://stackoverflow.com/questions/5863870/how-should-a-model-be-structured-in-mvc – Andrew Feb 10 '16 at 07:06

0 Answers0