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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:43:17+00:00 2026-06-13T07:43:17+00:00

Can someone please provide a way to handle the Fatal error: Call to a

  • 0

Can someone please provide a way to handle the Fatal error: Call to a member function prepare() on a non-object.

Here is my Scenario:

I have a Singleton Database Class which stores my database details and creates an instance of a PDO object when requested.

I then have the following two classes:

Base class:

namespace lib\translator;
use lib\database as db;

class EntityTranslator {
    protected $dbConn;

    public function __construct() {
        try {
            $this->dbConn = db\Database::getInstance();
        }
        catch(\PDOException $e) {
            // do some error logging here
        }
    }
}

Sub class:

namespace lib\translator;
use lib\entity as entity;

class RequestTranslator extends EntityTranslator {

    public function __construct() {
        parent::__construct();
    }

    public function createRequest() {
        $request = new entity\Request();
        try {
            $stmt = $this->dbConn->prepare("CALL createRequest()");
            $stmt->execute();

            $rowCount = $stmt->rowCount();
            if ($rowCount == 1) {
                $row = $stmt->fetch(\PDO::FETCH_ASSOC);

        // do stuff with the data here
            }
            return $request;
        }
        catch (\PDOException $pdoe) {
        // do error logging here
        }
    }
}

Now, I am not wondering why the error occurs. I am trying to handle the very rare case when my database connection is not available and the PDO object instantiation (done via the call to db\Database::getInstance()) throws an exception, resulting in the variable $dbConn remaining null.

I realise, it would be possible to test $dbConn for null before using it each time. But As I stated, this should be an exceptionally rare situation and may in fact never occur, so I don’t think that checking for null each time is a smart performance choice.

I would like to be able to add on another catch(\Exception $ex){} block to handle what is essentially a NullPointerException (as seen in Java) but PHP does not provide for such an exception. Rather they issue the Fatal error: Call to a member function prepare() on a non-object.

So what I am looking for, is a way to handle this error, for this situation, without checking for null each time.

Maybe I have missed the basic concept of error handling in PHP, but I have not been able to find a definitive resource on this topic.

Any suggestions?

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

    Why is your application allowed to call RequestTranslator::createRequest() anyway, when, at the time of instantiating RequestTranslator, you already know whether or not the database connection has instantiated properly?

    Shouldn’t your application logic be something like the following:

    try
    {
        /*
         * the following line should throw
         * SomeMeaningfulException when it 
         * is unable to connect to the database
         */
        $rt = new lib\translator\RequestTranslator();
    
        /*
         * this line should not be reached
         * if the above throws SomeMeaningfulException
         */
        $rt->createRequest();
    }
    catch( SomeMeaningfulException $e )
    {
        /* $rt database connection has not instantiated properly */
    }
    

    In other words, if you ask me, the constructor of RequestTranslator should already throw SomeMeaningfulException if it is unable to instantiate the database connection when the object’s methods are that dependant on functioning properly:

    class EntityTranslator {
        protected $dbConn;
    
        public function __construct() {
            try {
                $this->dbConn = db\Database::getInstance();
            }
            catch(\PDOException $e) {
                // do some error logging here
                throw new SomeMeaningfulException( 'some meaningful message' );
            }
        }
    }
    

    If this is somehow not preferred, then I don’t see why checking for null is that much of an issue. I agree with others, that the performance penalty is probably neglectable.

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

Sidebar

Related Questions

Can someone please tell me why I'm getting the error object reference is required
Can someone please provide an example of how to store, and read xml data
Can someone please provide a simple example of how to consume a REST service
Can someone please provide an example of using set_bound() method from Test::MockObject module? Can
Can someone please provide source on how to write a generic instead of trigger
Can someone please provide me with the workflow required to be able to simply
can someone please provide an example of how to use uBLAS product to multiply
Can someone please provide a simple example of what would cause a Git push
I'm new to this kabeja package so please can some one provide code example
Can someone please tell me what this means: 07-04 09:54:38.048: I/DetailActivity(15496): Title that is

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.