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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:53:09+00:00 2026-06-15T00:53:09+00:00

I am trying to optimize my PHP code today and I want to setupd

  • 0

I am trying to optimize my PHP code today and I want to setupd a singleton factory for PDO connections. Let me explain my current situation first.

I have a functions.php with a bunch of functions in it and at the top of almost every page I have

include 'functions.php';

Just like many do for their websites. One of these functions is my PDO connections, which looks like this:

function db_connect(){
$host = 'localhost';
$port = 3306; 
$database = 'mybigdb';
$username = 'user11';
$password = 'password1';

$dsn = "mysql:host=$host;port=$port;dbname=$database";

$db = new PDO($dsn, $username, $password);
return $db;
}

Within each of my functions that require a database connection, I will have something like this:

function fetch_row($param1, $param2, $db=NULL){
if(is_null($db))$db=db_connect();

//Rest of code here

}

That way I can pass along my database object, but have it added just in case it is not included. The code above works fine and I have been using it for a while. However, reading about Singleton Factory classes and thinking perhaps this is better.

I read this answer about Singleton factories but I am not quite sure how to use it. I am a nooby with classes and trying to learn. I have some questions…

  1. What would I put in the “…” where he has new ConnectionFactory(…); ?
  2. Where would I copy and paste the class code? Would I put it at the top of functions.php?
  3. What would I put at the beginning of each function? Should I still try and pass the database object through parameters?
  4. Is this method really worth converting too? Or is my current strategy Ok.

Thanks so much for an help 🙂

  • 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-15T00:53:10+00:00Added an answer on June 15, 2026 at 12:53 am

    You can create a singleton like this.

    class Connection {
      private static $_con = null;
      private function _constructor(){
          // here you instantiate de pdo conection
      }
      public static function GetInstace(){
          if ( self::$_con == null ) 
             self::$_con = new Connection();
          return self::$_con;
      }
    }
    

    After that you will do: $db = Connection::GetInstance();

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

Sidebar

Related Questions

I'm trying to optimize my code using Neon intrinsics. I have a 24-bit rotation
I'm trying to optimize my C++ code. I've searched the internet on using dynamically
I've been going back through all my old code and trying to optimize it.
I am trying to run the following code: 1. NSURL *checkLicenseURL = [NSURL URLWithString:@check_license.php?accesskey=&license_key=];
Just trying to optimize a SQL Query. SELECT Code, COUNT(*) FROM tblData WHERE ListID
I am trying to optimize my code with Cython. It is doing a a
I'm trying to optimize my PHP and MySQL, but my understanding of SQL databases
I'm running php code over ibm i series server. I've been trying to limit
On my local machine, I'm trying to deploy PHP code which was obfuscated using
Let me explain what I am trying to do, I am building a command

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.