I'm working on a project where I'd like to be able to declare a static member variable inside of an abstract base class. I've got a Model class, an intermediate Post class, and finally a site-specific Post class, something like the following:
abstract class Model {
protected static $_table = null;
protected static $_database = null;
...
}
abstract class PostModel extends Model {
public function __construct() {
if ( !isset(self::$_table) ) {
self::$_table = 'some_table';
}
parent::__construct();
}
...
}
class SitePostModel extends PostModel {
public function __construct() {
if ( !isset(self::$_database) ) {
self::$_database = 'some_database';
}
parent::__construct();
}
...
}
I'd like to make it apparent from the Model class that the $_table and $_database members are required. However, $_table is really static from the point of view of the PostModel class, and $_database is really static from the point of view of the SitePostModel class.
Is this a legitimate way to accomplish my goal? Does declaring the static variables in the Model itself imply that they should exist only once for the abstract base class, or only once for the actual instantiated class?
statickeyword and chapter about Late Static Binding.