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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:02:13+00:00 2026-05-16T15:02:13+00:00

I use a static function to create a PDO object. It accepts 2 params:

  • 0

I use a static function to create a PDO object.

It accepts 2 params:
a string and an object which contains the connection settings (dns, user, pass).

in order to prevent unnecessarily creating duplicate PDO connections with the same name, I tried to create a multi-key dictionary to cache the PDO object in.

Here is what I did:

include_once('IPDOSettings.php');

class PDOManager
{
    private static $connections; // array of connections

    public static function getConnection(IPDOSettings $settings, $connection_name = 'default')
    {
        $dictionary_key = array('name' => $connection_name, 'settings' => $settings);
        if(!self::$connections[$dictionary_key])
        {
            $DBH = new PDO($settings->getDNS(),$settings->getUser(),$settings->getPass());
            self::$connections[$dictionary_key] = $DBH;
        }
        return self::$connections[$dictionary_key];
    }

}

However after testing this I get this error Illegal offset type. After looking it up I find out that you cannot use objects or arrays as keys.

So is there anyway to do what I am trying to achieve?

  • 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-16T15:02:13+00:00Added an answer on May 16, 2026 at 3:02 pm

    Not really an answer to your question but do you expect PDOManager::getConnection() being called multiple times with the same $connection_name but different settings? Do you need to store the settings along with the db handle in your cache?

    This problem wouldn´t even occur if you´d just store the connections by name:

        // my suggestion/idea: use $connection_name as key
        $dictionary_key = $connection_name; 
    
        if(!self::$connections[$dictionary_key])
        {
            $DBH = new PDO($settings->getDNS(),$settings->getUser(),$settings->getPass());
            self::$connections[$dictionary_key] = $DBH;
        }
        return self::$connections[$dictionary_key];
    

    EDIT:

    Well, if you cant just use $connection_name as a key, you could combine $connection_name and use spl_object_hash() in order to get your key:

        $dictionary_key = $connection_name . spl_object_hash($settings); 
    

    This is much nicer then e. g. using serialize() to get a string representation of the $settings object.

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

Sidebar

Related Questions

I try to use getter/ setter with static function. Inside a mxml file, I
I am trying to use a PHP function from within a public static function
I'm wondering if I can use a static variable for optimization: public function Bar()
These three tests are identical, except that they use a different static function to
i use kohana framework and i am trying to code recursive function to create
How to create a function, which on every call generates a random integer number?
Why would I use a member function when I can pass a static function
I often use convenience functions that return pointers to static buffers like this: char*
Possible Duplicate: When to Use Static Classes in C# Questions in the title..........i would
Why cant i use static variables in events handling methods like this private void

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.