Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
The interface has a getter method. And abstract method implements this interface using 'return $this->property' in this method. How can I enforce classes that extend this to have this property set?
interface A {
public function getValue();
}
abstract class B implements A {
protected $value;
public function getValue() {
if (!isset($this->value)) { throw new Exception('message'); }
return $this->property;
}
}
abstract class B {
// no implement here
protected $value;
public function getValue() {
if (!isset($this->value)) { throw new Exception('message'); }
return $this->property;
}
}
class C extends B implements A{
public function setValue($value){
$this->value= $value;
}
}
Ups. Sorry for that one. It should nearly work, but. An abstract class should contain at least one abstract method. Mine doesn't as well as yours. So delete abstraction in first sample and it should work.
abstract class B implements A {
protected $value;
public function getValue() {
if (!isset($this->value)) { throw new Exception('message'); }
return $this->property;
}
// this will enforce implementation of method in classes that extend B
[color=#4000BF]abstract public function setValue($value);[/color]
}