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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:47:36+00:00 2026-06-18T07:47:36+00:00

I found a db adapter on php.net, on the mysql-connect page. This is a

  • 0

I found a db adapter on php.net, on the mysql-connect page.
This is a page viewed quite a lot (I would imagine).

It includes what the author claims is a singleton pattern class.

Found on http://php.net/manual/en/function.mysql-connect.php

Writer says:

“Here’s a singleton class to manage a single Database connection. The
Open method uses constant values as defautls, read from an ini file
through an initializer script loaded at the top of each web page. You
can also override the default values by manually entering your own (in
case you need to switch servers or database names mid-script). The qry
function takes a prepared statement and will return the first row,
first associative row, first cell, or entire result set based on the
second parameter (entire result if omitted).”

Usage: $DB = DB::Open();
$result = $DB->qry(” {SQL Statement} ;”);

<?php
    abstract class Database_Object
    {
        protected static $DB_Name;
        protected static $DB_Open;
        protected static $DB_Conn;

        protected function __construct($database, $hostname, $hostport, $username, $password)
        {
            self::$DB_Name = $database;
            self::$DB_Conn = mysql_connect($hostname . ":" . $hostport, $username, $password);
            if (!self::$DB_Conn) { die('Critical Stop Error: Database Error<br />' . mysql_error()); }
            mysql_select_db(self::$DB_Name, self::$DB_Conn);
        }

        private function __clone() {}

        public function __destruct()
        {
//            mysql_close(self::$DB_Conn);  <-- commented out due to current shared-link close 'feature'.  If left in, causes a warning that this is not a valid link resource.
        }
    }

    final class DB extends Database_Object
    {
        public static function Open($database = DB_NAME, $hostname = DB_HOST, $hostport = DB_PORT, $username = DB_USER,$password = DB_PASS)
        {
            if (!self::$DB_Open)
            {
                self::$DB_Open = new self($database, $hostname, $hostport, $username, $password);
            }
            else
            {
                self::$DB_Open = null;
                self::$DB_Open = new self($database, $hostname, $hostport, $username, $password);
            }
            return self::$DB_Open;
        }

        public function qry($sql, $return_format = 0)
        {
            $query = mysql_query($sql, self::$DB_Conn) OR die(mysql_error());
            switch ($return_format)
            {
                case 1:
                    $query = mysql_fetch_row($query);
                    return $query;
                    break;
                case 2:
                    $query = mysql_fetch_array($query);
                    return $query;
                    break;
                case 3:
                    $query = mysql_fetch_row($query);
                    $query = $query[0];
                    return $query;
                default:
                    return $query;
            }
        }
    }
?>

After looking at the code I have one question, is this really a singleton (see DB class and Open function)?

What about static properties makes a singleton pattern work ?

  • 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-18T07:47:37+00:00Added an answer on June 18, 2026 at 7:47 am

    I see what you mean, if self::$DB_Open DOES exist it sets it to null and creates a new instance of the object anyway.

    Appears you are right and this does not seem like the singleton pattern.

    Regarding singleton pattern with static properties. Unlike instance variables which are in the objects scope, static variables are in class scope.
    What this means is that even if you create an object multiple times it will still use the same value for the static property every time. It’s what allows them to be great for counters and therefore global singletons.

    See the below:

    Class A {
    static int y=1;
    int x=3;
    }
    $newA = new A();
    $newB = new A();
    $newC = new A();
    

    The above creates 3 instances of x. You can make a change in 1 instance and it will only make changes in that 1 Instance.
    But only 1 instance of y is created. So a change in 1 and all 3 will change. They all exist in same context. Therefore point to the same value. If you increment one the others will display the same increment ect..

    So to re-iterate, even though only a single instance of DB_Conn does exist it continually resets it and writes to it again (it does not have to act like this).

    To fix the problem the author could return the self::$DB_Open property if it does exist.

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

Sidebar

Related Questions

I have this code which I have found from google. It's a adapter for
Found this Multimap containing pairs? , but it is not much help How would
I'm developing a sync adapter. I found this: http://groups.google.com/group/android-developers/msg/85f9304dfcc4e284 In that forum a google
Found this while reading the Neo4j manual, specifically here , I found the sentence:
Found This: tyty stack, Social Icons not working with Infinite Scrolling on Wordpress I'm
found this little code snippet that seems to do what i want, but im
Found this: Sub SurroundWithAppendTag() DTE.ActiveDocument.Selection.Text = .Append( + DTE.ActiveDocument.Selection.Text + ) End Sub But
I am trying to compile one of the projects found here USB-I2C/SPI/GPIO Interface Adapter.
I have found this example Lazy load of images in ListView from Fedor which
I Try to connect a Bluetooth Adapter, which send me some Data after sending

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.