微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在没有循环或条件的情况下打印1到1000 – 在PHP中

here开始的想法 – 这次你只能使用PHP.
这有可能吗?
这是一个基于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,$method);
      echo "$count ";
      $this->{"print" . ++$count}();
   }
}

class thousand_printer extends printer {
   public function print1001() {}
}

new thousand_printer;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐