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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:04:14+00:00 2026-05-13T11:04:14+00:00

I have a few classes that perform some MySQL queries and prepared statements. However,

  • 0

I have a few classes that perform some MySQL queries and prepared statements. However, I am lost in how to incorporate my PDO object within those classes. For example, I want to do something like this:

<?php

$dbh = new PDO(...);

class Foo extends PDO {
    public $dbh;

    public function bar() {
        $this->dbh->prepare('SELECT * FROM table');
        $this->dbh->execute();

    }
}


?>

Unfortunately, it doesn’t work. Can anyone suggest an elegant way to do this? Thanks for your time. Sorry I’m new to this, please leave any comments if you are unclear about anything and I’ll do my best to respond!

  • 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-13T11:04:15+00:00Added an answer on May 13, 2026 at 11:04 am

    You can instantiate your connection to the database in a class that implement the singleton pattern.
    The connection will be done once and this class will be easily accessible by all of your other objects / scripts.

    i use a class called “Core” in the following example;

    class Core
    {
        public $dbh; // handle of the db connexion
        private static $instance;
    
        private function __construct()
        {
            // building data source name from config
            $dsn = 'pgsql:host=' . Config::read('db.host') .
                   ';dbname='    . Config::read('db.basename') .
                   ';port='      . Config::read('db.port') .
                   ';connect_timeout=15';
            // getting DB user from config                
            $user = Config::read('db.user');
            // getting DB password from config                
            $password = Config::read('db.password');
    
            $this->dbh = new PDO($dsn, $user, $password);
        }
    
        public static function getInstance()
        {
            if (!isset(self::$instance))
            {
                $object = __CLASS__;
                self::$instance = new $object;
            }
            return self::$instance;
        }
    
        // others global functions
    }
    

    this class take parameters from a static class called “Config” where you can store your configuration:

    <?php
    class Config
    {
        static $confArray;
    
        public static function read($name)
        {
            return self::$confArray[$name];
        }
    
        public static function write($name, $value)
        {
            self::$confArray[$name] = $value;
        }
    
    }
    
    // db
    Config::write('db.host', '127.0.0.1');
    Config::write('db.port', '5432');
    Config::write('db.basename', 'mydb');
    Config::write('db.user', 'myuser');
    Config::write('db.password', 'mypassword');
    

    in all your scripts / objects you just have to get the instance of Core and then query the DB

    $sql = "select login, email from users where id = :id";
    
    try {
        $core = Core::getInstance();
        $stmt = $core->dbh->prepare($sql);
        $stmt->bindParam(':id', $this->id, PDO::PARAM_INT);
    
        if ($stmt->execute()) {
            $o = $stmt->fetch(PDO::FETCH_OBJ);
            // blablabla....
    

    If you need more information about singleton look at the PHP doc http://php.net/manual/en/language.oop5.patterns.php

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

Sidebar

Related Questions

i have a few classes that i am trying to move to using generics
I have a few classes such that: public class XMLStatusMessage extends XMLMessage {} public
I have a few classes, such as those that outline database table structure or
I've gotten to the point where I have made a few classes that I
I have a few classes which do nothing except in their constructors/destructors. Here's an
I have a few model classes with basic one-to-many relationships. For example, a book
I need to convert several Java classes to C#, but I have faced few
Currently I have my VaryByCustom functionality implemented in classes that implement an interface IOutputCacheVaryByCustom
I have a script that appends some rows to a table. One of the
I have a question regarding how to call ruby classes that reside outside of

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.