Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Why is your application allowed to call <code>RequestTranslator::createRequest()</code> anyway, when, at the time of instantiating <code>RequestTranslator</code>, you already know whether or not the database connection has instantiated properly?</p> <p>Shouldn't your application logic be something like the following:</p> <pre><code>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-&gt;createRequest(); } catch( SomeMeaningfulException $e ) { /* $rt database connection has not instantiated properly */ } </code></pre> <p>In other words, if you ask me, the constructor of <code>RequestTranslator</code> should already throw <code>SomeMeaningfulException</code> if it is unable to instantiate the database connection when the object's methods are <em>that</em> dependant on functioning properly:</p> <pre><code>class EntityTranslator { protected $dbConn; public function __construct() { try { $this-&gt;dbConn = db\Database::getInstance(); } catch(\PDOException $e) { // do some error logging here throw new SomeMeaningfulException( 'some meaningful message' ); } } } </code></pre> <p>If this is somehow not preferred, then I don't see why checking for <code>null</code> is <em>that</em> much of an issue. I agree with others, that the performance penalty is probably neglectable.</p>
 

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