I'm doing a php MVC project using code igniter. I have two models, a and b.
Each class contains four functions (insert, delete, update and view) and their implementations are almost the same with each other. A friend suggested to put generic versions of the four functions inside a utility class and the two models can access the function their. For example, here is the generic implementation of insert.
public function insert($tableName, $columns, $preds = null)
{
// call code igniter's insert function
}
// do the same with delete, update and view
However, having been introduced to data directed programming before and having recently found out php can have arrays of functions, I think we can make this better.
What I would like to do is: On each class, I am going to create a global array that holds the four code igniter CRUD functions specific to the class with the keys "insert", "add", "delete", "update".
Rather than create four functions for each CRUD operations, I am going to create one utility function and one general execute function for each class.
Note: I am extremely new to php, some of the php code might not work. I have not implemented this directly yet.
// in the utility class
public function execute($dataFunctions, $op, $tableName, $columns)
{
// this will not work, I am not yet familiar with putting functions inside an array
$dataFunctions['op']($tableName,$columns);
}
// on class a
public function execute($op, $columns)
{
// dataFunctions is the name of the global array
utils->execute($dataFunctions,$op,"a",$colums)
}
// same with class b
Thing is, I am not completely sure that this is a good idea. I haven't done this before. I might not be aware of any drawbacks. Should I prefer the way with the array of functions? Or should I just stick to implementing the four crud functions?
$dataFunctions[$op]($tableName,$columns);