【问题标题】:fatal error call to undefined function对未定义函数的致命错误调用
【发布时间】:2014-05-11 03:39:55
【问题描述】:

我已经写了这段代码,

<?php
class fizzbuzz{
    function mod3($angka)
    {
        $a = $angka % 3;
        if($a==0) return true;
        else return false;
    }
    function mod5($angka)
    {
        $b = $angka % 5;
        if($b==0) return true;
        else return false;
    }
    function index(){
        for ($i=1; $i < 101; $i++) { 
            if(mod3($i) == true && mod5($i) == true){
                echo "fizzbuzz, ";
            }else if(mod3($i) == true){
                echo "fizz, ";
            }else if(mod5($i) == true){
                echo "buzz, ";
            }else echo $i.", ";
        }
    }
}
$show = new fizzbuzz;
$show->index();
?>

然后它想出了这个错误

致命错误:在第 19 行调用 C:\xampp\htdocs\tes-bimasakti\fizzbuzz.php 中未定义的函数 mod3()

请帮我解决这个错误..

【问题讨论】:

    标签: xampp


    【解决方案1】:

    你忘了$this-&gt;:

        if($this->mod3($i) == true && $this->mod5($i) == true){
           ^^^^^^^--- here            ^^^^^^^---here
    

    没有$this-&gt;,php 正在寻找一个顶级的全局函数。它不会在你的对象中寻找方法。

    【讨论】:

      【解决方案2】:

      使用this关键字

      function index(){
          for ($i=1; $i < 101; $i++) { 
              if($this->mod3($i) == true && $this->mod5($i) == true){
                  echo "fizzbuzz, ";
              }else if($this->mod3($i) == true){
                  echo "fizz, ";
              }else if($this->mod5($i) == true){
                  echo "buzz, ";
              }else echo $i.", ";
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-10
        • 1970-01-01
        相关资源
        最近更新 更多