I have wrote simple class that allowing database selection and inserts.Could you please tell me does this a right way to use __call method with any useful advantage of it ?
<?php
class _getDatabase
{
public $db;
public function __construct(PDO $connection)
{
$this->db = $connection;
}
public function __call($function, $args) {
if ($function == 'getSelect') {
$sql = $this->db->prepare($args[0]['query']);
$sql->execute(array(
$args[0]['parameters']
));
return $sql->fetchALL(PDO::FETCH_OBJ);
}
if ($function == 'getInsert') {
$sql = $this->db->prepare($args[0]['query']);
$result = $sql->execute($args[0]['parameters']);
return $result;
}
}
}
$dbh = new PDO("mysql:host=127.0.0.1;dbname=" . DATABASE, USERNAME, PASSWORD, array(
PDO::ATTR_PERSISTENT => true
));
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Set Errorhandling to Exception
$dbFactory = new _getDatabase($dbh);
$select = array();
$insert = array();
$select['query'] = 'SELECT * FROM `branches` WHERE B_CODE = ?';
$select['parameters'] = 'AMB';
$insert['query'] = 'INSERT INTO `commition`(`date`, `reg`, `com`, `oper`) VALUES (?,?,?,?)';
$insert['parameters'] = array('2014-02-20','86869','DITEC','Asuntha');
var_dump($dbFactory->getSelect($select));
var_dump($dbFactory->getInsert($insert));
?>