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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:26:11+00:00 2026-05-15T15:26:11+00:00

I’ve been a procedural programmer for over 4 yrs and it’s time to start

  • 0

I’ve been a procedural programmer for over 4 yrs and it’s time to start looking into OOP. With that said, let’s say I needed to call two methods in my class. Each method requires a connection to the DB so that’s two trips to the DB, which also opens multiple connections.
Can this be avoided by having some sort of code in the application layer (constructor?) or does a connection pool have to be setup on the DB side? And just for kicks, I’m not using mysql; I’m using mongodb with codeigniter.

Here’s what I have so far, not sure if it’s ideal to use?

Here’s where I setup my DB info:

database_conn.php

class Database_Conn extends Model {

    function _connect() {
        $m = new Mongo("localhost:27017", array("persist"=>"x"));
        $db = $m->selectDB( "foo" );
        return $db;
    }    
}     

sample model file

class Home_model extends Model {

    public function __construct() {
        // Establish connection to "profiles" table
        $this->db_conn = Database_Conn::_connect()->selectCollection( "profiles" );
    }

    function getMyProfile($username) {
        $data = $this->db_conn->findOne(array("username" => $username) );
        return $data;
    }

    function getAll() {
        $data = $this->db_conn->find(); 
        return $data;
    }
}
  • 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-15T15:26:12+00:00Added an answer on May 15, 2026 at 3:26 pm

    you should use singleton pattern

    EDIT:
    the way you did it, it is possible to call _connect multiple times, which means reconnecting.

    singleton implementation usually means you have to make constructor private/protected and define a getInstance method which creates connection on first call and returns the created connection on later calls.

    this is what i would do:

    class Database_Conn extends Model {
    
        static protected $_instance;
    
        protected $db = null;
    
        final protected function __construct() {
            $m = new Mongo("localhost:27017", array("persist"=>"x"));
            $this->db = $m->selectDB( "foo" );
        }
    
        static public function getInstance() {
            if (!(self::$_instance instanceof self)) {
                self::$_instance = new self();
            }
            return self::$_instance;
        }
    
        public function getConnection() {
            return $this->db;
        }
    
        final protected function __clone() { }
    }
    

    and then use Database_Conn::getInstance()->getConnection() to get the connection object.

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

Sidebar

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.