Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>While avoidance of repetition (DRY) is a principle that should always factor into your coding decisions, it should be achieved without violating another important principle, which is the separation of concerns (<a href="http://en.wikipedia.org/wiki/Separation_of_concerns" rel="nofollow">SoC</a>, see also <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle" rel="nofollow">SRP</a>). Your example, databaseDo($action), is dual-purpose: it (1) instantiates a database connection and (2) it executes a query. </p> <p>Now, you may say, 'Yes! That is just what I want! Kill two birds with one stone!', and your reason for saying so would be understandable. However, mixing responsibilities can become problematic because, when you have to make changes to the way in which one responsibility is handled, you will likely also have to make changes to the way the other responsibility is handled. </p> <p>Imagine you were, at some point down the road, required to support two database connections instead of just one. Suppose one of the two databases supports transactions on tables for which the other does not. Your databaseDo() function will first have to negotiate to which database to connect, and then, in order to safely execute the 'do' action, some transaction support testing will be required. It would look something like this:</p> <pre><code>$context = 'production'; // but it could equally be 'development' function databaseDo($action) { $db_hostname = ($context == 'production') ? 'http://remotehost.com' : 'localhost'; $db_username = ($context == 'production') ? 'apache' : 'root'; $pass = ($context == 'production') ? 'productionpassword' : 'developmentpassword'; try { $dbh = new PDO("mysql:host=$db_hostname;dbname=mysql", $db_username, $db_password); echo 'Connected to database'; if($context == 'production') { // ... some complicated logic to determine whether the production db // will support your query, then execute it if so, exit if not ... } if($context == 'development') { // ... some more complicated logic for testing and querying the // development db ... } $dbh = null; } catch(PDOException $e) { echo $e-&gt;getMessage(); } } </code></pre> <p>Additional requirements for handling one responsibility will add complexity to the handling of the second responsibility, and this function will become more and more difficult to maintain.</p> <p>A better approach to DRY in this scenario would be to handle database connection management in one component, such as within a context-aware singleton class instance (a common approach), and the query handling in another component. Thus, your query function would not necessarily have to change on account of a change in database connection handling. The tutorial to which you referred has <a href="http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html#13" rel="nofollow">instructions for creating and using such a singleton instance</a>.</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