Situation
Consider I’ve got the following class structure:
// a user model
class UserModel extends BaseModel {
protected static $table = 'Users';
}
// a controller for the user model
class UserModelController extends BaseModelController {
protected static $model = 'UserModel';
}
// a base model
class BaseModel {
protected static $table = '';
}
// a base model controller
class BaseModelController {
protected static $model = '';
}
What I want
What I want to do now is to have basic functionality in both base controller and base model, for example adding the function delete() into the BaseModelController:
public static function delete($id) {
$stmt = Database::prepare('DELETE FROM `' . $table . '` WHERE `id`=?');
$res = $stmt->execute(array($id));
}
The problem
How do I get the table name? Theoretically I am searching for something like the following:
$table = static::$model::$table;
But, unfortunately this does not work. Is this even possible?
You need to save
static::$modelinto a temporary variable: