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 8147029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:12:33+00:00 2026-06-06T14:12:33+00:00

I was thinking through some DAO design stuff with a teammate and came across

  • 0

I was thinking through some DAO design stuff with a teammate and came across an interesting question about which I have yet to find any best practices guidelines.

When creating a DAO object, when should the DB connection occur? We’ve talked through instantiating an object that creates a DB connection in a constructor (then uses it in subsequent methods) and making a static class that establishes a connection and tears it down after every method call.

Both have clear performance implications based on environment – a static object would be good for a few intermittent calls whereas instantiating an object that holds a connection would be great for a number of calls in a short amount of time for a small number of users.

Is there another way to do this? Perhaps something that would be better for performance across most situations?

  • 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-06-06T14:12:34+00:00Added an answer on June 6, 2026 at 2:12 pm

    Since you have tagged this as mvc, I will assume that your DAOs already exist within the model layer. As i understand it. the DAOs are responsible for for same task as Data Mappers, and provide the information from storage (which might or might not be SQL database) to your Domain Objects within the Model Layer.

    The best way to deal with this issue would be to have factory, which create the data access objects within your model layer. This would have several advantages:

    • decoupling from the class names of different DAOs
    • ability to know when first DAO is initialized
    • sharing single DB connection between all DAO instances

    This is how I would implement something like it:

    class DaoFactory
    {
        protected $connection = null;
        protected $provider = null;
    
        public function __construct( $provider )
        {
            $this->provider = $provider;
        }
    
        public function create( $name )
        {
            if ( $connection === null )
            {
                $connection = call_user_func( $this->provider );
            }
            $instance = new $name;
            $instance->setConnection( $connection );
            return $instance
        }
    }
    

    And the usage would look something like this:

    $connectionProvider = function()
    { 
        $instance = new \PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 
                             'username', 'password');
        $instance->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        $instance->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
        return $instance;
    };
    
    $daoFactory = new DaoFactory( $connectionProvider );
    

    Then you pass the $daoFactory variable to whichever object will require to create DAO. This way, if need arises, you can easily replace the factory (as long as it implements same create() method footprint), with something completely different. And your domain business logic is in no way impacted by such a change.

    The objects which use this factory became completely unaware that there even exists a database connection, and that DAOs need it to function. And you can even end up with situations, when you don’t even need to establish a connection to database.

    Rant
    Since you have data access objects on your codebase, it would indicate that you are trying to use OOP. Or at least something similar. But the static classes/functions/variables are not part of OOP paradigm. If you have a class with static-only methods and few static variables, what you have actually made are just simple php function with global variables, wrapped in a namespace, which just happens to look like class.

    PHP community tends to call this COP (class oriented programming) as a joke, but essentially it is procedural code.

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

Sidebar

Related Questions

I am having some troubles thinking through a design issue and thought that the
I was going through some Amazon interview questions on CareerCup.com, and I came across
I was looking through some code on a project of mine and thinking about
I’m thinking through some database design concepts and believe that creating sample data simulating
I'm thinking about starting a new project using EF 4 and going through some
I've been thinking about this object oriented design question for a while now and
While thinking about this question and conversing with the participants, the idea came up
I am thinking through a nice pattern to be useful across domains of measurable
I have just done something without thinking it through. I have made a branch
Im thinking about building an application on our website to track order places through

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.