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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:41:45+00:00 2026-05-21T06:41:45+00:00

Suppose I have this: class Validator { private $db; public checkIfUsernameAlreadyExists($username) { if (!$this->db)

  • 0

Suppose I have this:

class Validator {
  private $db;

  public checkIfUsernameAlreadyExists($username) {
    if (!$this->db)
      return false;

  // Queries
  }
}

Assume the $db-object was created in the constructor (or a $db-object was given as a parameter in the constructor).

The problem is that all methods which have to use the $db-object need to check first if this object really exists. A database-connection may fail for several reasons. If it doesn’t exist and no check was made, the script will crash (“method on non-object”-error).

Is there a way to work around this issue? Checking the object in every method doesn’t sound the correct way. Or is it?

Thank you

  • 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-21T06:41:46+00:00Added an answer on May 21, 2026 at 6:41 am

    Checking the object in every method is definitely not right.

    The purpose of constructor arguments is to have a valid object to work with after it is instantiated:

    class Validator {
      private $db;
    
      public function __construct(PDO $db) {
         // validate $db here
      }
    
      public checkIfUsernameAlreadyExists($username) {
         $this->db->query('SELECT * FROM table'); // exception thrown here
         // never reaches here
      }
    }
    
    try {
        $validator = new Validator(new PDO('mysql:dbname=db', 'user', 'pass'));
        $validator->checkIfUsernameAlreadyExists('foo');
    } catch (PDOException $e) {
        echo 'Database error occured: ', $e->getMessage();
        exit(1);
    }
    

    So, you validate $db once in the constructor and that’s it. If the database connection fails, then the $db object should (and will if it’s PDO) throw an exception which will halt the execution of the method anyway.

    Note: I’d discourage the use of Singleton pattern or global variables for a lot of reasons that can easily be found on Stack Overflow or Google.

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

Sidebar

Related Questions

I have a user model like this: class User < ActiveRecord::Base validates :password, :presence
let's say that I have this function in Rails (I have actually :P -
I have two models. class User has_one :spec, :dependent => :destroy # returns true
I'm having a hard time with StructureMap configuration. I have a ValidationProvider that accepts
I've implemented some validation in my form by extending the form.VTypes class to include
class ABC is an abstract base class. class X is its subclass. There's some
Our C# application produces reports of various types. Each type of report has an
I'm supposed to make a simple application, an xHTML editor with some basic validation
I'm just not understanding why things are being resized when I call the validate()

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.