Roman Toasov Roman Toasov - 1 month ago 7
PHP Question

Convert IF to more OOP approach in method

Cannot think of any way to convert this IF's used to see number of params for

bindParam()
into some something more in OOP friendly.
Can something other than using
switch
statement be done here?
Is it good OOP practice to have his IFs in method anyway?

Ad-Hoc Polymorphism perhaps?

class MySQL_Query {

protected $dbh;

public function __construct($dbh) {
$this->dbh = $dbh;
}

public function SelectQuery($query, $param) {

$PDOStatement = $this->$dbh->PDO->prepare($query);

if (isset($param[3])) {
$PDOStatement->bindParam($param[0], $param[1], $param[2], $param[3]);
} elseif (isset($param[2])) {
$PDOStatement->bindParam($param[0], $param[1], $param[2]);
} else {
$PDOStatement->bindParam($param[0], $param[1]);
}

$PDOStatement->execute();

$result = array();

while ( $row = $statement->fetch(PDO::FETCH_NAMED) ) {
$result[] = $row;
}

return $result;

}

}


EDIT My Solution here is what i was asking for how to remove
IF
, as i dont' want to pass params to
execute()
directly because they treated as
PDO::PARAM_STR
and you cannot change that. Here is how i eliminated ugly
IF
i translated it to
ternary IF
, looks way more readable now and shorter.

$PDOStatement->bindParam( $param[0], //parameter
$param[1], //variable with value
!empty($param[2]) ? $param[2] : null, //data_type
!empty($param[3]) ? $param[3] : null, //length
!empty($param[4]) ? $param[5] : null //driver_options
);

Answer

PDOStatement::execute accepts an array of parameters

unnamed parameters

/* Execute a prepared statement by passing an array of insert values */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < ? AND colour = ?');
$sth->execute(array($calories, $colour));

named parameters

/* Execute a prepared statement by passing an array of insert values */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < :calories AND colour = :colour');
$sth->execute(array(':calories' => $calories, ':colour' => $colour));

variable number of parameters using IN query

/* Execute a prepared statement using an array of values for an IN clause */
$params = array(1, 21, 63, 171);
/* Create a string for the parameter placeholders filled to the number of params */
$place_holders = implode(',', array_fill(0, count($params), '?'));

/*
    This prepares the statement with enough unnamed placeholders for every value
    in our $params array. The values of the $params array are then bound to the
    placeholders in the prepared statement when the statement is executed.
    This is not the same thing as using PDOStatement::bindParam() since this
    requires a reference to the variable. PDOStatement::execute() only binds
    by value instead.
*/
$sth = $dbh->prepare("SELECT id, name FROM contacts WHERE id IN ($place_holders)");
$sth->execute($params);

source: PDOStatement::execute docs