An "abstract" DBObject class
Posted: Mon Apr 19, 2010 2:14 pm
Hi everyone.
I'm developing my own little PHP Framework. I want to create a generic DBObject class and then extend it to create, for example, User and News class. Now all my classes have something like:
All classes have this structure. So, why not create a parent "abstract" class? With a $tableName property, for example. What stop me are all these static functions. I can't use static functions in the parent class because then I can't use object properties ($tableName) inside... So I'm searching for some tricks... I want to write classes like:
And do:
Suggestions? Thanks, Lorenzo. 
I'm developing my own little PHP Framework. I want to create a generic DBObject class and then extend it to create, for example, User and News class. Now all my classes have something like:
Code: Select all
class User {
public $id = 0;
public $name = null;
private function __construct() { }
public static function create() {
return new User();
}
public static function get($id) {
$q = "SELECT * FROM users WHERE id = $id";
return mysql_fetch_object($q, 'User');
}
public static function getAll() {
$q = "SELECT * FROM users";
return mysql_fetch_object($q, 'User');
}
}Code: Select all
class User extends DBObject {
public $name = null;
private function __construct() {
parent::__construct('users', 'id'); // DB table and ID column
}
}Code: Select all
$u = User::get(23);
echo $u->name;
$users = User::getAll();
$new_u = User::create();