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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:25:03+00:00 2026-06-01T00:25:03+00:00

I have to share an instance of a config object in PHP. This object

  • 0

I have to share an instance of a config object in PHP.
This object contains db username and db password. The db user can change. So I have to share te object instance.

Example.

Default db user is ‘default’. default has only rights to SELECT data from db.

When website user logges in on website as admin, db user has to change to ‘admin’. admin has rights to SELECT, UPDATE, DELETE etc.

When web user logges in as a normal user, db user has to change to ‘normal’ normal has rights as SELECT, UPDATE etc.

I’ve to share the config instance. What is the safest way to share this object?

  • 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-01T00:25:05+00:00Added an answer on June 1, 2026 at 12:25 am

    You could make use of the singleton pattern in order to keep one instance accessible from anywhere within your application.

    I also suggest you have a singleton as a connection-handler where you can get, set and maintain connections, in which you also store the login credentials.

    Example:

    <?php
    class ConnectionManager
    {
        private $hostname;
        private $username;
        private $password;
    
        private $connections = array();
    
        private static $instance = null;
    
        public static function getInstance ()
        {
            if (self::$instance == null)
                self::$instance = new self();
            return self::$instance;
        }
    
        private function __construct ()
        {}
    
        public function newConnection ($name, $database)
        {
            // Connect to database using predefined credentials
    
            $this->connections[$name] = new PDO();
        }
    
        public function __get ($name)
        {
            if (!array_key_exists($name, $this->connections))
                die("Could not get connection: $name");
    
            return $this->connections[$name];
        }
    }
    
    // Create a new connection named database1
    ConnectionManager::getInstance()->newConnection('database1', 'database_name');
    
    // Load connection database1
    ConnectionManager::getInstance()->database1->prepare("SELECT * FROM table");
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a music events website and want to have a 'share this event'
I'm using php 5.2 with IIS7.5. I have a network share on a NAS
Is there a way to make one (1) instance of an object and share
What is a good way to share an instance of an object between several
I have written a C++ class that I need to share an instance of
I have to share my source code with someone else but I don't want
I have a share button on my website, I understand that the link is
Say you have 2 share libraries, lib1.so and lib2.so, that both have libcommon.a statically
I'm writing a Windows Service in C#. I have a samba share on a
I have four classes which share some arrangement of four properties. I have currently

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.