这是一个基于PHP重载的有趣oo解决方案:
class thousand_printer {
public function __construct() {
$this->print1();
}
public function __call($method, $_) {
$count = str_replace('print', '', $method);
echo "$count ";
$this->{"print" . ++$count}();
}
public function print1000() {
echo "1000\n";
}
}
new thousand_printer;
我很高兴我的解决方案如此受欢迎.这是一个略微改进,提供了一些模块化:
class printer {
public function __construct() {
$this->print1();
}
public function __call($method, $_) {
$count = str_replace('print', '', $method);
echo "$count ";
$this->{"print" . ++$count}();
}
}
class thousand_printer extends printer {
public function print1001() {}
}
new thousand_printer;