如果您想更改通用默认值,我可以看到避免更新大量值的唯一方法是:
abstract class Parent{
const DEFAULT_SELECT_ORDER = "desc";
public abstract function select($order = "");
protected static function select_order(&$order)
{
if (empty($order) || !in_array(strtolower($order), array("asc", "desc"))) {
// additional test to check if the value is valid
$order = self::DEFAULT_SELECT_ORDER;
}
}
}
class Child extends Parent{
public function select($order = "") // here is the problem error
{
self::select_order($order);
// selection code
}
}
嗯 - 另一种可能更好的方法:
abstract class Parent {
protected $order = "desc";
public function order($order) {
if (in_array(strtolower($order), array("asc", "desc"))) {
$this->order = $order;
} else {
// probably should throw an exception or return false or something
}
return true;
}
public abstract function select();
}
class Child extends Parent {
public function select() {
// select code using $this->order
}
}
$query = new Child();
$query->order("asc");
$results = $query->select();