加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 13:09:33 所属栏目:PHP教程 来源:网络整理
导读:从 here开始的想法 – 这次你只能使用PHP. 这有可能吗? 这是一个基于PHP重载的有趣oo解决方案: class thousand_printer { public function __construct() { $this-print1(); } public function __call($method,$_) { $count = str_replace('print','',$met
从 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 "1000n";
   }
}

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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读