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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:21:35+00:00 2026-06-02T01:21:35+00:00

This is my general php page: <?php require_once(‘includes.php’); require_once(‘cms.class.php’); ….. rest of the page

  • 0

This is my general php page:

<?php
require_once('includes.php');
require_once('cms.class.php');
.....
rest of the page
?>

in includes.php an pro object called $db is initiated which I want to use in the class specified in the cms.class.php

includes.php:

$db = new PDO('mysql:host=localhost;dbname=xxxxx','xxxxx','xxxxxx'); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

How can I use this database object in my classes without having multiple places where my credentials are stored?

  • 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-02T01:21:36+00:00Added an answer on June 2, 2026 at 1:21 am

    You need want a dependency manager or a bootstrapper or whatever you want to call it.

    class Dependency_Manager {
    
        private $db;
    
        public function __construct($settings) {
            $this->db = new PDO('mysql:host=localhost;dbname=' . settings["dbname"],settings["username"],$setings["password"]); 
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
            $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
        }
    
        public function getDB() {
            return $db;
        }
    
    }
    
    class CMS {
        public function __construct(PDO $db) {
            /* .. */
        }
    }
    
    $setting = array(/* etc */);
    
    $dm = new Dependency_Manager($settings);
    $cms = new CMS($dm->getDB());
    

    This approach scales very well and can handle any dependecy. It also aims to put all the settings in one place so you don’t have configuration settings littered everywhere. The $dm is the only one who knows the settings, if you need to construct something based on the settings, put it in the $dm.

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

Sidebar

Related Questions

this is a General Question about MVC .. I wrote a PHP Class that
I'm trying to learn the code igniter library and object oriented PHP in general
My q and a page sucks - it's this: http://rankingclimber.com/qa.php I checked out http://www.text-link-ads.com/r/faq
Here is my directory listing : >htdocs -->MyWeb ---->admin ------>cms.php ---->images ---->controller ------>post.php ------>general.php
This is a general question. I have a website with a page showing many
Yesterday I asked this general question about decimals and their internal precisions. Here is
From my understanding of the docs this general approach should work: begin try1 rescue
This is a general question about MVC as a pattern, but in this case
This is a general question concerning technology decisions for a product development. My aim
This is a general question. And may not be specific to datagrids. How can

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.