Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP/PDO: how to handle Fatal error: Call to a member function prepare() on a non-object
    primarykey
    data
    text
    <p>Can someone please provide a way to handle the <code>Fatal error: Call to a member function prepare() on a non-object</code>.</p> <p>Here is my Scenario:</p> <p>I have a Singleton Database Class which stores my database details and creates an instance of a PDO object when requested.</p> <p>I then have the following two classes:</p> <p><strong>Base class:</strong></p> <pre><code>namespace lib\translator; use lib\database as db; class EntityTranslator { protected $dbConn; public function __construct() { try { $this-&gt;dbConn = db\Database::getInstance(); } catch(\PDOException $e) { // do some error logging here } } } </code></pre> <p><strong>Sub class:</strong></p> <pre><code>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-&gt;dbConn-&gt;prepare("CALL createRequest()"); $stmt-&gt;execute(); $rowCount = $stmt-&gt;rowCount(); if ($rowCount == 1) { $row = $stmt-&gt;fetch(\PDO::FETCH_ASSOC); // do stuff with the data here } return $request; } catch (\PDOException $pdoe) { // do error logging here } } } </code></pre> <p>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 <code>db\Database::getInstance()</code>) throws an exception, resulting in the variable <code>$dbConn</code> remaining <code>null</code>.</p> <p>I realise, it would be possible to test <code>$dbConn</code> for <code>null</code> 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 <code>null</code> each time is a smart performance choice.</p> <p>I would like to be able to add on another <code>catch(\Exception $ex){}</code> block to handle what is essentially a <code>NullPointerException</code> (as seen in Java) but PHP does not provide for such an exception. Rather they issue the <code>Fatal error: Call to a member function prepare()</code> on a non-object.</p> <p>So what I am looking for, is a way to handle this error, for this situation, without checking for <code>null</code> each time. </p> <p>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.</p> <p>Any suggestions?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload