PHP abstract properties

Solution 1:

There is no such thing as defining a property.

You can only declare properties because they are containers of data reserved in memory on initialization.

A function on the other hand can be declared (types, name, parameters) without being defined (function body missing) and thus, can be made abstract.

"Abstract" only indicates that something was declared but not defined and therefore before using it, you need to define it or it becomes useless.

Solution 2:

No, there is no way to enforce that with the compiler, you'd have to use run-time checks (say, in the constructor) for the $tablename variable, e.g.:

class Foo_Abstract {
  public final function __construct(/*whatever*/) {
    if(!isset($this->tablename))
      throw new LogicException(get_class($this) . ' must have a $tablename');
  }
}

To enforce this for all derived classes of Foo_Abstract you would have to make Foo_Abstract's constructor final, preventing overriding.

You could declare an abstract getter instead:

abstract class Foo_Abstract {
  abstract public function get_tablename();
}

class Foo extends Foo_Abstract {
  protected $tablename = 'tablename';
  public function get_tablename() {
    return $this->tablename;
  }
}