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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:17:51+00:00 2026-05-31T18:17:51+00:00

PHP PDO Singleton Class: <?php require_once(‘app/config.php’); // Require constants HOST, DATABASE, USER, PASSWORD /*

  • 0

PHP PDO Singleton Class:

<?php

require_once('app/config.php'); // Require constants HOST, DATABASE, USER, PASSWORD

/*

dbConnection class.

Manages connections to and operations on the database. Call dbConnection::getInstance() to return an instance.
Prepare your statements by calling prepareQuery() on the object

Attribute list:

$instance:
> Static self instance to manage database resource

$connection:
> Holds connection resource

$sth:
> Statement handler variable. Handles SQL statements.
_______________________________________________________________________________________________________________

Method list:

getInstance():
> Creates or returns existing connection to the database

prepareQuery():
> Prepares the $sth variable for execution.

bindParameter():
> Binds parameters to the $sth variable.

numRows($query):
> Returns the number of returned from a query

runQuery():
> Executes the current statement on the database

fetchRow():
> Executes the current statement then returns an associative array

fetchObj($className, $parameters = NULL):
> Executes the current statement and returns an object of your specification. Also takes additional parameters to pass to the object's constructor.


*/

class dbConnection
{   
    private static $instance = NULL;
    private $connection;
    private $sth;

    function __construct()
    {
        $this->connection = new PDO('mysql:host=' . HOST . ';dbname=' . DATABASE, USER, PASSWORD);
    }

    function getInstance()
    {
        if (self::$instance == NULL)
            self::$instance = new dbConnection();
        return self::$instance;
    }

    function prepareQuery($query)
    {
        $this->sth = $this->connection->prepare($query);
    }

    function bindParameter($number, $value)
    {
        $this->sth->bindParam($number, $value);
    }

    function numRows()
    {   
        try
        {
            $this->sth->execute();

            $count = $this->sth->rowCount();
            return $count;
        }
        catch(PDOException $e)
        {
            echo __LINE__.$e->getMessage();
        }
    }

    function runQuery()
    {
        try
        {
            $this->sth->execute() or print_r($connection->errorInfo());
        }
        catch(PDOException $e)
        {
            echo __LINE__.$e->getMessage();
        }
    }

    function fetchRow()
    {
        try
        {
            $this->sth->setFetchMode(PDO::FETCH_ASSOC);
            $this->sth->execute();
            return $this->sth;
        }
        catch(PDOException $e)
        {
            echo __LINE__.$e->getMessage();
        }
    }

    function fetchObj($className, $parameters = NULL)
    {
        try
        {
            $this->sth->setFetchMode(PDO::FETCH_CLASS, $className, $parameters);
            $this->sth->execute();
            return $this->sth;
        }
        catch(PDOException $e)
        {
            echo __LINE__.$e->getMessage();
        }
    }
}
?>

How to test singleton?
Take an object at a time, which is closed when you are finished with the object at the end.

  • 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-31T18:17:52+00:00Added an answer on May 31, 2026 at 6:17 pm

    I think you are misapplying the Singleton pattern here.

    Nevertheless, testing Singletons is possible. Quoting Testing Code that uses Singletons

    PHPUnit has a backup/restore mechanism for static attributes of classes.

    This is yet another feature of PHPUnit that makes the testing of code that uses global state (which includes, but is not limited to, global and superglobal variables as well as static attributes of classes) easier.

    Also see http://www.phpunit.de/manual/current/en/fixtures.html#fixtures.global-state

    The @backupStaticAttributes annotation that is discussed in the section called “@backupStaticAttributes” can be used to control the backup and restore operations for static attributes. Alternatively, you can provide a blacklist of static attributes that are to be excluded from the backup and restore operations like this

    So if you wanted to disable the backup, you’d do

    class MyPdoTest extends PHPUnit_Framework_TestCase
    {
        protected $backupStaticAttributesBlacklist = array(
          'dbConnection' => array('instance')
        );    
    
        // more test code
    }
    

    Also have a look at the chapter on Database Testing

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

Sidebar

Related Questions

from http://www.php.net/manual/en/class.pdo.php ###### config.ini ###### db_driver=mysql db_user=root db_password=924892xp [dsn] host=localhost port=3306 dbname=localhost [db_options] PDO::MYSQL_ATTR_INIT_COMMAND=set
Should I use php PDO or normal mysql_connect to execute database queries in PHP?
I am using PDO with PHP to create a new database and then a
I'm reworking some PHP code to use PDO for the database access, but I'm
According to the PDO documentation , you can store database login details in php.ini,
Is it possible to extend PHP PDO statement class to add custom methods to
When using PHP PDO to access the database, is there a way to list
i have simple singleton that sets pdo object from factory class , in the
I'm using the following php pdo code to insert data into mysql database, the
The dbcred.php class <?php # pdo_testdb_connect.php - function for connecting to the test database

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.