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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:14:55+00:00 2026-06-06T18:14:55+00:00

I have started to learn how to use OOP and have created a user

  • 0

I have started to learn how to use OOP and have created a user authorisation class to check if the user exists etc. Currently I am connected to the database by using the global variable $dbh which is a PDO connection. I’ve heard that using global variables in this way isn’t good practice but am not sure how I can improve it, would I just pass the $dbh variable into the method that requires it when connecting to a database and why exactly is this not considered good practice?

Here is some code I am using:

Database PDO connection included in calling program:

//test the connection
    try{
        //connect to the database
        $dbh = new PDO("mysql:host=localhost;dbname=oopforum","root", "usbw");

    //if there is an error catch it here
    } catch( PDOException $e ) {
        //display the error
        echo $e->getMessage();
    }

The class that requires the database connection:

class Auth{

        private $dbh;

        function __construct(){

            global $dbh;
            $this->dbh = $dbh;
        }

        function validateLogin($username, $password){

            // create query (placing inside if statement for error handling)
            if($stmt = $this->dbh->prepare("SELECT * FROM oopforumusers WHERE username = ? AND password = ?")){

                $stmt->bind_param(1, $username);
                $stmt->bind_param(2, $password);
                $stmt->execute();


                // Check rows returned
                $numrows = $stmt->rowCount();

                //if there is a match continue
                if( $numrows > 0 ){
                    $stmt->close();
                    return TRUE;
                }else{
                    $stmt->close();
                    return FALSE;
                }

            }else{
                die('ERROR: Could not prepare statement');
            }

        }


        function checkLoginStatus(){

            if(isset($_SESSION['loggedin'])){

                return TRUE;

            }else{

                return FALSE;

            }

        }

        function logout(){

            session_destroy();
            session_start();

        }
    }
  • 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-06T18:14:56+00:00Added an answer on June 6, 2026 at 6:14 pm

    You should pass the PDO connection to the constructor:

    function __construct($dbh) {
        $this->dbh = $dbh;
    }
    

    The connection is called a dependency of your class because obviously your class needs it in order to carry out its function. Good practice dictates that your class should make it explicit that this dependency exists; this is achieved by making it a mandatory constructor parameter.

    If you instead pull in the dependency from a global variable you create several issues:

    • It’s not clear at all to a user of your class that there is a dependency in the first place
    • Your class is now coupled to the global variable, which means that the variable cannot be removed or renamed without breaking the program
    • You have created the preconditions for “action at a distance”: modifying the value of the global variable causes another (seemingly unrelated) part of your application to change behavior
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started to learn about python and is currently reading through a script
I'm currently starting to learn to use java to create dynamic websites. I've started
I've just started to learn about tie . I have a class named Link
I have started playing with CodeIgniter now. And I use their user guide and
I have started to learn ANTLR and have both the 2007 book The Definitive
greetings, today i have started to learn java using netbeans ide. i would like
I have just started to learn how to code iOS apps. I have made
I have recently started to learn Objective-C and write my tests using OCUnit that
I am building an app to learn Django and have started with a Contact
Hello I'm very new to OpenGlES and i have started to try to learn

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.