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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:39:09+00:00 2026-05-24T02:39:09+00:00

Continuing my journey into more advanced OO, I’m trying to come up with the

  • 0

Continuing my journey into more advanced OO, I’m trying to come up with the best way of being able to create a class called database that can happily use the soon-to-be-deprecated mysql_ commands, the mysqli_ library, and perhaps further down the line, other flavours of database as well.

I’d like my calling code to look something like this, for simplicity:

$db = new database("MYSQL", $host,$user,$password,$databaseName); 
$db->query("SELECT myField FROM myTable");

while ($ROW = $db->fetch_assoc($QRY)) {
  echo $ROW['myField'] . "<br/>";
}

When instantiating the database object, thats the part where i’d hope to specify MYSQL, MYSQLI, MSSQL, or whatever I feel like adding later.

I appreciate I could achieve exactly what I want by implementing switch blocks in every method of database:

class database() {

  function __construct($type,$host,$user,$password,$databaseName) { 

    $this->dbType = $type; 

    switch ($type) {

      case "mysql":
        mysql_dao::connect($host,$user,$password,$databaseName);
        break;

      case "mysqli":
        mysqli_dao::connect($host,$user,$password,$databaseName);
        break;

      case "mssql":
        mssql_dao::connect($host,$user,$password,$databaseName);
        break;

      //other cases
    }

  }

  function query($SQL) {  
    switch ($this->dbType) { 
       case "mysql": mysql_dao::query($SQL); break;
       case "mysqli": mysqli_dao::query($SQL); break; 
       case "mssql": mssql_dao::query($SQL); break;
    }
  }

  function fetch_assoc($SQL) {  
    switch ($this->dbType) { 
       case "mysql": mysql_dao::fetch_assoc($SQL); break;
       case "mysqli": mysqli_dao::fetch_assoc($SQL); break; 
       case "mssql": mssql_dao::fetch_assoc($SQL); break;
    }
  }

  //other methods....

}

…But this seems incredibly messy. Every time a method of database was called, the code is checking what type it is and calling the method from a different object.

So my next solution was simply to omit database at all, and just use calling code that looks like this:

$db = new mysql_dao($host,$user,$password,$databaseName); 
$db->query("SELECT myField FROM myTable");

while ($ROW = $db->fetch_assoc($QRY)) {
  echo $ROW['myField'] . "<br/>";
}

…and so we just call the database access object for the type of database we’re using, but I don’t like that either, if I wanted to shift a whole project from mysql to mysqli, i’d have to seek out all of these object references and change them. Although this is fairly trivial in a modern PHP editor, it still doesn’t feel like it should be the best way (i’m starting to get theoretical instead of practical, now!)

So finally, i’d hoped that I could build the database class with a single switch statement:

class database {

    function __construct($type,$host,$user,$pass,$db) {    

        switch ($type) {

            default:
            case "MYSQL":
                return new mysql_dao($host,$user,$pass,$db);
                break;

            case "MYSQLI":
                return new mysqli_dao($host,$user,$pass,$db);
                break;

            case "MSSQL":
                return new mssql_dao($host,$user,$pass,$db);
                break;    
        }    

    }

}

… and then just implement an interface with all of the other methods being in the classes for the individual database types. Unfortunately this isn’t working either because even though the constructor for database returns an instance of another object, I can’t call database->query because the method doesn’t exist in the database object.

As far as I could work out, extending database with mysql_dao / mysqli_dao etc. classes wasn’t right either, since you have to call the extended class to use its methods, and I want to call the parent class but use the methods of the child class. So this suggests that database should extend the dao classes, but you can’t have one child class with multiple potential parents (can you?)

To conclude – I am almost sure I am missing something, and that what I want to do should be both possible and quite straightforward, and I just haven’t thought of the right way – can anyone give me a pointer?

  • 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-24T02:39:10+00:00Added an answer on May 24, 2026 at 2:39 am

    Despite the fact that what you’re doing is reinventing the wheel (take a look at PDO), in general, for cases like this you might want to use something like the factory pattern.

    In basic pseudo-code:

    $db = Database::create('mysql', $host, ...); // gets a MySQLDatabase object
    $db = Database::create('mysqli', $host, ...); // gets a MySQLiDatabase object
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Continuing on my attempt to create a DateTime class , I am trying to
Continuing my journey into the world of variadic templates , I encountered another problem.
Continuing from this question : When I am trying to do fopen on Windows,
Continuing my journey of using Udacity's python-based CS212 and translating to Clojure, I am
Continuing the discussion from Understanding VS2010 C# parallel profiling results but more to the
Continuing my explorations in NHibernate with my podcast application, I've come across something odd:
Continuing investigation on a embedded WindowsMediaPlayer problem , i am trying to do simple
Continuing on my previous question , I'm trying to initialize a session-scoped JSF bean
Continuing from the previous question: Power Series in Haskell I am trying to write
Continuing the question in: Keep windows trying to read a file Thanks to accepted

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.