
Originally Posted by
DougBTX
Why not just use inheritance?
Douglas
I am using inheritance, but I suspect you question is "Why not just use inheritance the normal way?. Consider how it would look for the "or empty" version compared with the normal version (ignoring constuction):
PHP Code:
class FieldInArraySpecification extends Specification {
function isSatisfiedBy($test) {
$check_funct = $this->check;
if (!$ret = $check_funct($test->get($this->field), $this->set)) {
$this->log->push($this->msg, $this->logdesc);
}
return $this->returnValue($test, $ret);
}
}
class FieldInArrayOrEmptySpecification extends Specification {
function isSatisfiedBy($test) {
if (!$test->get($this->field))
return $this->returnValue($test, true);
$check_funct = $this->check;
if (!$ret = $check_funct($test->get($this->field), $this->set)) {
$this->log->push($this->msg, $this->logdesc);
}
return $this->returnValue($test, $ret);
}
}
the entire block of
PHP Code:
$check_funct = $this->check;
if (!$ret = $check_funct($test->get($this->field), $this->set)) {
$this->log->push($this->msg, $this->logdesc);
}
return $this->returnValue($test, $ret);
is repeated for both sections of the code. This contrasts with what I implemented:
PHP Code:
class FieldInArraySpecification extends Specification {
function isSatisfiedBy($test) {
if (preg_match('/orEmpty/i', get_class($this) && !$test->get($this->field))
return $this->returnValue($test, true);
$check_funct = $this->check;
if (!$ret = $check_funct($test->get($this->field), $this->set)) {
$this->log->push($this->msg, $this->logdesc);
}
return $this->returnValue($test, $ret);
}
}
class FieldInArrayOrEmptySpecification extends FieldInArraySpecification {}
Less typing, less duplication of code. More confusing? Not sure.
Bookmarks