I’m working on a website and I used CodeIgniter as my framework, but I’m getting increasingly frustrated with the way they abuse OOP. I’m not a fan of OOP but when I use it I prefer a decent implementation. So I’ve decided to quickly build a PHP framework. Anyways, I’m stuck with the usual MVC architecture for my application but I was wondering what is a good way of passing around models to controllers and libraries. I would prefer to provide an instance of a model to the constructor of a controller. Something along these lines
class MyController implements IController //whatever
{
public MyController(IUrlRouteModel url_route_model, IUserModel user_model)
{
//usual assignment
}
public some_action()
{
UserEntity users = user_model->find_all();
}
}
I’m using PHP but this example/question is not directly linked to just PHP, the answer should tho. Now the real question is, where in my could would I manage the instance of the models, where could/should I create them. I will probably call the controller somewhere in the \Router but passing every model to the constructor of the router would be strange. Is there someone who can shed some light on this subject. I’ve tried to find out how other frameworks do this and this is what i have so far
- They use a ModelKeeper that functions as some type of factory for the models, I kind off like this option but the source of that class will get messy
- The models are provided in static context. Either every method of the model is static, or they use some type of singleton pattern.
Share your experience stack-overflow? What would be a good way to implement this? Or shorter, what would be an appropriate place to instantiate the models and how?
PS
I guess the model would look something like this
class UserModel implements IUserModel
{
public UserModel(DatabaseConnection dbc)
{ /*store for later use*/ }
}
You should pass an instance of
ModelFactoryto the controller, which the is used inside of controller(s). TheModelFactoryclass in the constructor should require a DB connection ( most likely – aPDOinstance ). You create instance of it in bootstrap phase ( might be where your\Routeris called too ) and , if you need something like that, you can provide the factory with object responsible for caching too.API would go something like this :
The factory itself looks sort of like this:
Your factory can also just receive configuration for DB connection and initialize the PDO object only when it is first time required. It also might be a better choice if you want an ability to juggle connection to multiple databases.
P.S. code was not tested, it was all made up and might contain both syntax and logical
fucmess-ups.