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

  • Home
  • SEARCH
  • 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 7599141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:35:15+00:00 2026-05-30T22:35:15+00:00

I’m working on a website and I used CodeIgniter as my framework, but I’m

  • 0

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

  1. 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
  2. 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*/ }
}
  • 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-30T22:35:17+00:00Added an answer on May 30, 2026 at 10:35 pm

    You should pass an instance of ModelFactory to the controller, which the is used inside of controller(s). The ModelFactory class in the constructor should require a DB connection ( most likely – a PDO instance ). You create instance of it in bootstrap phase ( might be where your \Router is 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 :

    namespace Application;
    
    class FooController
    {
        protected $factory = null;
    
        public function __construct( \Framework\ModelFactory $factory )
        {
            $this->factory = $factory;
        }
    
        public function bar_action( \Framework\Request $request )
        {
            $model = $this->factory->create( 'User' );
            // then pass this model to your view object 
        }
    }
    

    The factory itself looks sort of like this:

    namespace Framework;
    
    class ModelFactory
    {
        protected $connection = null;    
    
        public function __construct( \PDO $connection )
        {
           $this->connection = $connection;
        }
    
        public function create( $name )
        {
            $name = '\\Persistence\\' . $name; 
            // you probably will want configure this in some way
    
            $model = new $name( $this->connection );
            $model->preconfig(); // .. hell .. maybe you need
            return $model;
        }
    }
    

    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.

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

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I

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.