I'm trying to get the code below to work in child classes. But it keeps failing because it is checking the basicDbClass rather than the child class.
For those complaining and voting my question down because of my use of static methods and variables, the static getBy method serves the purpose of returning an array of matching class objects. If you have a complaint about that methodology, then rather voting down every question that uses code you don't agree with, look for the next question about that subject and then give them your brilliant answer. If you're really antsy, I'll even do you the favor of asking that question so that you can get this out of your system.
The main problem is in static::$data_table. When I do the following, I get a fatal error:
access to undeclared static property: basicDbClass::$data_table
$obj_array = childClass::getBy('id',5);
class basicDbClass {
public static function getBy($property,$value) {
if(!property_exists(get_called_class(),$property) ) {
debug::add('errors',__FILE__,__LINE__,__METHOD__,$property . ' is not a valid property.');
return false;
}
$obj_array = array();
dbConnection::connect(MAIN_DB);
$sql = sprintf('SELECT `id` FROM `%s` WHERE `%s` = \'%s\'',static::$data_table,mysql_real_escape_string($property),mysql_real_escape_string($value));
$result = mysql_query($sql);
if($result === false) {
return false;
}
while($row = mysql_fetch_assoc($result)) {
$obj_array[$row['id']] = new static($row['id']);
}
return $obj_array;
}
}
class childClass extends basicDbClass {
protected static $data_table = 'child_class_table';
protected $name;
protected $id;
}

childClassis a subclass ofbasicDBClassand edit your question to reflect that? – Abhi Beckert Aug 19 '12 at 4:57mysql_*functions. – teresko Aug 19 '12 at 7:31