I have a class that performs basic MySQL operations. This is all in PHP.
class dbTables {
public $name;
protected $fields = array(); // array of dbTableField objects
public $result_sets = array();
protected $primary_key; // filled using getFields() from __construct()
public function __construct($table_name) {
$this->name = $table_name;
$this->getFields();
}
public function removeWhitespace($table_name) {
$this->name = $table_name;
$this->getFields();
}
// etc...
}
So all you need is the table name to create the object, and construct automatically gets a bunch of field data from INFORMATION_SCHEMA
(names, data types, etc).
I also have a method for removing whitespace from a column It's currently located in another class, but I'm thinking of moving into the above dbTables
class. You can see the method below. I don't think it's really necessary to read the method to answer my question, but it could be helpful.
protected function removeWhiteSpace($target_field,$abbrev_field,$from_size,$to_size,$options) {
// optional conditions based on original character length and resulting character length.
/* options: from_size = all/longer_than/shorter_than
* to_size = all/longer_than/shorter_than
*/
$temp_table = $this->table_name;
$sql = "UPDATE `$temp_table`
SET `undo_sku` = `$target_field`, `$target_field` = REPLACE(`$target_field`,' ',''), `$abbrev_field` = 'w'";
$where = array();
if($options['from_size'] == 'longer_than') {
$where[] = "`$target_field` > $from_size";
} else if($options['from_size'] == 'shorter_than') {
$where[] = "`$target_field` < $from_size";
}
if($options['to_size'] == 'longer_than') {
$where[] = "LENGTH(REPLACE(`$target_field`,' ','')) > $to_size";
} else if($options['from_size'] == 'shorter_than') {
$where[] = "LENGTH(REPLACE(`$target_field`,' ','')) < $to_size";
}
//WHERE `$target_field` = '' AND LENGTH(REPLACE(`vnd_sku`,' ','')) <= $string_length";
$result = mysql_query($sql);
}
Now, if I do move it into dbTables
, I will still want to use the functionality in the other class. Should I just instantiate a dbTables
object whenever I need to use removeWhitespace
? Or, could I give dbTables
its own removeWhitespace
method like this:
protected function removeWhiteSpace($target_field,$abbrev_field,$from_size,$to_size,$options) {
/* options: from_size = all/longer_than/shorter_than
* to_size = all/longer_than/shorter_than
*/
$dbtable = new dbTables($this->table_name);
$temp_table = $this->table_name;
$result = $dbtable->removeWhiteSpace($target_field,$abbrev_field,$from_size,$to_size,$options);
return $result;
}
The advantage I see is that now I have a way to limit instantiation of the dbTables
object in various other classes. Methods that need to use removeWhiteSpace
can use $this->removeWhiteSpace
instead of instantiating a dbTables
object. The class's own removeWhiteSpace
method takes care of that.
I'm kind of new to OOP, though, so I'm just wondering if that is good practice or bad.
I also hope I'm asking this question in the right place. I'm pretty sure it's not a StackOverflow question.