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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:12:46+00:00 2026-06-14T04:12:46+00:00

Writing my first application in PHP that makes use of classes and objects. I

  • 0

Writing my first application in PHP that makes use of classes and objects.
I have a DB class which takes a string to select the appropriate config because there are multiple databases. I started out with a login class but scratched that idea in exchange for a user class so I can do user->isLoggedIn stuff. The user class uses MySQL which stores user and login information, as well as credentials for the second database.

$mysql = new db( 'mysql' );

$user = new user( $mysql );
if( !( $user->isLoggedIn() === true ) )
{
    goToPage( 'login.php' );
    exit();
}

The second database is Sybase and stores account information. I need the credentials from the user class to get lists and accounts information from here. I think an account class should be next but not sure of the best way to do it. Something like this maybe..

$sybase = new db( 'sybase' );

$account = new account( $sybase );

$account_list = $account->getList( $user->info );

$user->info being an array i guess of credentials and info needed for the account table, or is there a better way to go about this?

Edited to include db class example

config.db.php

$config_array = array();

$config_array[ 'mysql' ][ 'dsn' ] = "mysql:host=localhost;dbname=********;charset=UTF-8";
$config_array[ 'mysql' ][ 'user' ] = "********";
$config_array[ 'mysql' ][ 'pass' ] = "**************";

$config_array[ 'sybase' ][ 'dsn' ] = "odbc:DRIVER={Adaptive Server Anywhere 8.0};***************";
$config_array[ 'sybase' ][ 'user' ] = "**********";
$config_array[ 'sybase' ][ 'pass' ] = "*********";

class.db.php

public function __construct( $type )
{
    require 'config.db.php';

    $this->type = $type;

    foreach( $config_array[ $this->type ] AS $key => $value )
    {
        $this->$key = $value;
    }

    try
    {
        $this->connection = new PDO( $this->dsn, $this->user, $this->pass, $this->options );
    }
    catch( PDOException $ex )
    {
        log_action( "db->" . $this->type, $ex->getCode() . ": " . $ex->getMessage() );
        $this->error = true;
    }
    return $this->connection;
}

Does something like the following make sense?

class User()
{
    public function getAccountList()
    {
        $this->Account = new Account( new Database( 'sybase' ) );
        return $this->Account->list( $this->userid );
    }
}

Also, Accounts have different sections (i.e. History & Notes, Financial Transactions, Documents) that will be different ‘tabs’ on the page. Should each one of those be a class too?

  • 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-14T04:12:47+00:00Added an answer on June 14, 2026 at 4:12 am

    First up, as a secondary answer on the other answer by Dimitry:

    The things you’ll gain by having a Singleton pale to the things you lose. You get a global object, which every part of your program can read and modify, but you lost testability, readability, and maintainability.

    Because the Singleton object is in fact global, you cannot accurately isolate the single units in your application (which is crucial for Unit Testing), because your function is dependant on other components, which are “magically” inserted into it.

    You lose readability because method() may actually need other things to work (for instance, the user object needs a db instance, this makes new user($db) much more readable than new user(), because even without looking at the source code, I can tell what the method/object needs to work.

    You lose maintainability because of the reason stated above as well. It’s harder to tell which components are inserted via the Singleton than it is to see it in the function’s “contract”, for that reason, it’ll be harder for future you and/or any other developer to read and refactor your code.


    As a side note, it’s considered good naming convention to name Class names with its first letter upper cased, I’ll be using this convention from now on.

    Let’s start from the top. You have 2 possible states for your Db object, MysqlDb, and SybaseDb. This calls for polymorphism. You have an abstract class Db, and two concrete classes MysqlDb and SybaseDb inheriting from it. The instantiation of the correct Db object is the responsibility of a factory

    class DbFactory {
    
        private $db;
    
        /**
         * Create a new Db object base on Type, and pass parameters to it.
         *
         * @param string $type Type of database, Mysql or Sybase.
         * @param string $dsn  The DSN string corresponding to the type.
         * @param string $user User credential
         * @param string $pass Password credential
         *
         * @return Db
         */
    
        public function create($type, $dsn, $user, $pass) {
            if (!is_a($this->db, "Db")) {
                $type     = $type . "Db";
                $this->db = new $type($dsn, $user, $pass);
            }
    
            return $this->db;
        }
    }
    
    abstract class Db {
    
        /**
         * @param $dsn
         * @param $user
         * @param $pass
         */
        public function __construct($dsn, $user, $pass) {
            $this->db = new PDO("$dsn", $user, $pass);
            $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        }
    }
    
    class MysqlDb extends Db {
    
        /*
         * More specific, Mysql only implementation goes here
         */
    }
    
    class SybaseDb extends Db {
    
        /*
         * More specific, Sybase only implementation goes here
         */
    }
    

    Now you should ask yourself, who is responsible for getting the list of accounts? Surely they shouldn’t fetch themselves (just as much as it isn’t the user’s responsibility to fetch its own data from the database). It’s the User‘s responsibility to fetch these accounts, using the SybaseDb connection.

    In fact, the User needs the following to work:

    • Sybase database connection – to fetch Account list. We’ll pass the DbFactory instance to it, so that we can easily get the instance if it were already instantiated, and instantiate it if not.
    • State information (logged in status, user ID, user name, etc).

    The User isn’t responsible to set this data, it needs it in order to work.

    So the User constructor should look like this (assuming “ID” and “name” fields):

    User::__construct($id, $name, DbFactory $factory);
    

    The User will have a field $accounts, which would hold an array of Account objects. This array would be populated with a User::getAccounts() method.

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

Sidebar

Related Questions

I'm writing a PHP application which makes use of a modular structure. To modules
I am currently writing my truly first PHP Application and i would like to
I have just made my first proper little desktop GUI application that basically wraps
I'm writing my first iPhone application and I'm having trouble switching views. I have
I'm writing a PHP function that would use a table of sorts to look
I have a web application, written in PHP that incorporates Javascript and JQuery, that
I'm writing my first web application (still!), and have a problem activating user accounts.
I am writing a PHP/MySQL application that maintains a masterlist of user preferences and
Writing my first php application. I'm stuck on how to add either a loop
I'm writing an application in PHP that uses a MySQL database to store information.

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.