I am setting up a content management system and I am using classes to connect to database. The problem I’m having is with abstraction/inheritance (not sure which one it’s called). I have 3 classes: department, content & users. All three of these classes use similar methods except the only difference is the table for which they are pulling from.
Assuming I already have a class set up for database connection and it’s assigned a $database variable, I would like to make one parent class that has all the methods each of the three children will need but each child will declare it’s own table name in a static variable.
[php]
// parent class
class ParentObjects {
protected function create()
{
global $database;
$sql = "INSERT INTO ".self::$tblName." ("id", "name", "etc") VALUES ("1", "bob", "etc")";
$database->query($sql);
}
// additional methods using the child's table name
}
[/php]
I have a few more methods in the parent class and that is just an example but say I have a User class that uses the create function:
[php]
// user class
class User extends ParentObjects {
protected static $tblName = "user";
// additional user only methods
}
[/php]
How could I use the ParentObjects code but without declaring the $tblName variable until the child class? Is there such a thing as child::$tblName? I can’t do User::$tblName because there is also Content and Department.